C++ Unit Test in Visual Studio 2012 - c++

I am working with Microsoft Visual Studio 2012 Ultimate to write C++ applications. I got that version from my MSDNAA access. My Problem is that I want to create unit tests for the C++ classes I wrote.
Please Note:
It's standard conform C++, nothing mixed, no C#, it's just C++ that can also be compiled with the g++.
Under file -> new -> project -> Visual C++ exists something like a "managed testproject":
However when I create such a project I cannot manage it to add references e.g. to "MyClass.h" and to compile. And I cannot find a simple tutorial for that.
Can anyone help me by showing how to set up a simple C++ Unit Test with Visual Studio 2012?

You have two choices for C++ unit tests Manage Test Project and Native Unit Test Project. You should select the native one, and then just add the includes you want and write the tests.
Here is a dummy example where I include a "foo.h" header, instantiate a foo and call one of its methods.
#include "stdafx.h"
#include "..\foo.h" // <- my header
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
foo f;
Assert::AreEqual(f.run(), true);
}
};
}
See Unit testing existing C++ applications with Test Explorer for more.

Related

OpenCASCADE 7.6.0 not compiling with a .NET 6.0 class library with Visual Studio 2022 (Windows 10)

Steps to reproduce:
Install a version of Visual Studio (I used VS Community 2022). Install OpenCASCADE 7.6.0.
Create a C++ .NET CLR project using Visual Studio 2022 targeting .net6.0.
Change settings to include OpenCASCADE header and library files.
Edit the main header by replacing the code within it with below:
#pragma once
//for OCC graphic
#include <OpenGl_GraphicDriver.hxx>
//wrapper of pure C++ classes to ref classes
#include <NCollection_Haft.h>
namespace ClrClsLibDotNetCoreMwe {
public ref class Class1
{
// TODO: Add your methods for this class here.
};
}
Attempt to build.
Issue: The build fails with the following complain:
1>C:\OpenCASCADE-7.6.0-vc14-64\opencascade-7.6.0\inc\NCollection_DefaultHasher.hxx(34,1): error C2872: 'HashCode': ambiguous symbol
1>C:\OpenCASCADE-7.6.0-vc14-64\opencascade-7.6.0\inc\NCollection_DefaultHasher.hxx(34,1): message : could be 'HashCode'
1>C:\OpenCASCADE-7.6.0-vc14-64\opencascade-7.6.0\inc\NCollection_DefaultHasher.hxx(34,1): message : or 'System::HashCode'
What fixes the problem:
Either Targeting .NET Framework instead of .NET Core (/clr instead of /clr:netcore).
Or removing one of the headers.
Please see if there is a way where I can keep both the headers and target .NET Core?
I have looked around for a possible solution before posting this question here. A promising solution was to disable implicit usings. However, that didn't pan out.
I had the same problem.
In my case, the "using namespace System;" included in the header file. The text caused the problem.
Thanks!

Default code in each new cpp files in Visual Studio 2015

I am currently studying C++ using Visual Studio and I don't want to use a new project for each and every small topic that I go through, instead, I have one project into which I Add and Remove Cpp files as needed.
I wonder if there is a possibility to set a default piece of code into each newly added Cpp file so that I donĀ“t need to copy it every time. Some simple code like this
#include <iostream>
using namespace std;
int main()
{
cin.get()
return 0;
}

Visual Studio Test Explorer not detecting Boost Tests

I'm trying to get into testing in c++ using VS2015 with Boost, following this guide on docs.microsoft.
I've installed the boost unit test adapter from the marketplace.
If my understanding is correct, I should now be able to see my boost test cases in the test case explorer within VS2015 but somehow I don't see any of them. Building the project runs just fine and depending on whether my test cases are valid or not, the program exits with 0 or 201.
My project has a single .cpp file with the following contents:
#define BOOST_TEST_MODULE MyTest
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(my_boost_test)
{
BOOST_CHECK(0==0);
}
BOOST_AUTO_TEST_CASE(my_boost_test2)
{
BOOST_CHECK(0==1);
}

Visual Studio (2013) Unit Test fails with C0000005

I just set up a new (native) unit test in a new Solution. But even the example program does not work if I add an Assertion. It compiles without problems and the default generated (empty) test succeeds. But when I add a simple Assertion it fails with error code C0000005.
This is probably related to a similar issue, but I don't even have some library connected and therefore can't use the proposed solutions.
The test looks like this:
#include "stdafx.h"
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace Test_Native
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
Assert::AreEqual(1, 1, L"message", LINE_INFO()); // Without this line everything is fine
// TODO: Your test code here
}
};
}
This is a known bug. Unfortunately, Microsoft considers this as "Won't Fix".
In short, there are two workarounds:
Compile the actual project in release mode and the test project in debug mode.
Outsource all testable functions to a static library project.

Visual studio for c++?

I have installed visual studio express 2010 c++. However when trying to follow a beginners book and make the hello world program, visual studio opens up a project with this in it:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
The thing is, this looks nothing like the c++ in the book i have just started reading (C++ Without Fear). In fact, if i type 'cout',even after entering using namespace std, cout is not found.
This is the books example:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "Never fear, C++ is here!";
return 0;
}
Am i missing something here?
Create Windows (or Win32, don't remember exactly) C++ Console Application project, don't select C++/CLI project type.
C++/CLI is very special language, which is used only for interoperability between managed and unmanaged code. It is better to forget about C+++/CLI at this stage...
As Alex said start with C++ Win32 console project, but select empty project so that the IDE don't fill things out automatically! Go to Source Files in your Solution Explorer, right click on it and add new item and choose C++ file (call it main.cpp for example).
At that point you should be ready to go.
Try this sample code that I prepared for you...
#include <iostream>
using namespace std;
int main(char *argv[], int argc) {
cout << "Hello World!" << endl;
return 0;
}
It should print out Hello World! in the console.
You want to start with a Visual C++ Win32 Console Application.
If you want to create your own files completely (i.e. no stub file with a main defined), you can check "Empty Project" in the options of the Win32 Application Wizard.
This is not C++. This is so called "managed" C++. Basically, a totally different language by Microsoft that is compatible with their .NET platform. For example, you can mix C# code and managed C++ code in single binary. This technology is very Microsoft specific and is not portable to any other compiler/OS (even Mono, which is C# for Linux, doesn't have it). See Managed Extensions for C++ for more details.