Visual Studio (2013) Unit Test fails with C0000005 - c++

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.

Related

gMock Visual Studio test crashes when using EXPECT_CALL

When running my test from the Test Explorer in Visual Studio 2022 [17.2.0], testhost.exe crashes when my unit test has a gMock EXPECT_CALL call in it. What am I doing wrong?
Minimal code:
#include <CppUnitTest.h>
#include <CppUnitTestAssert.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using ::testing::Return;
class MockCow
{
public:
MOCK_METHOD0(Moo, int());
};
TEST_CLASS(the_unit_test)
{
TEST_METHOD(the_test_method)
{
MockCow cow;
EXPECT_CALL(cow, Moo())
.WillOnce(Return(42));
cow.Moo();
}
};
If the EXPECT_CALL call is commented out, the unit test will not crash.
I created this project by making an Empty C++ project, then installing the NuGet gMock package, latest stable version 1.11.0, and adding library directories/dependencies in the project settings. The project configuration type is Dynamic Library (.dll).
For C/C++ Additional Include Directories, I have:
$(VCInstallDir)Auxiliary\VS\UnitTest\include;
For Linker Additional Library Directories, I have:
$(VCInstallDir)Auxiliary\VS\UnitTest\lib;
For Linker Input Additional Dependencies, I have:
x64\Microsoft.VisualStudio.TestTools.CppUnitTestFramework.lib
It compiles and links successfully, so the above is just for completeness. I have also tried installing previous versions of the gMock NuGet behavior, but I get the same behavior.
Faulting module name: MyTest.dll_unloaded, version: 0.0.0.0, time stamp: 0x62cd8255
Exception code: 0xc0000005
Fault offset: 0x000000000011e8f7
Faulting process id: 0x8b04
Faulting application start time: 0x01d895fa0d052ac3
Faulting application path: C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\Extensions\TestPlatform\testhost.exe
There are other posts about gMock crashes that may be tangentially related, but they either don't have solutions or are missing details and were never updated.
You mix the very different test frameworks.
The more important thing is that you do not initialize Google test framework.
TEST_CLASS, TEST_METHOD are of another test framework, do not perform required pre and post stuff, Google mock is not supposed to work there.
Choose one framework and use it.
#include <gmock/gmock.h>
#include <gtest/gtest.h
using ::testing::Return;
class MockCow
{
public:
MOCK_METHOD0(Moo, int());
};
TEST(the_unit_test, the_test_method) {
MockCow cow;
EXPECT_CALL(cow, Moo())
.WillOnce(Return(42));
cow.Moo();
};
int main(int argc, char *argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Dealing with multiple projects in visual studios

I am trying to figure out how to deal with multiple projects under the banner of a single solution and how to correctly link projects in order to make things work in conjunction.
I began by making two projects in one solution - for simplicity's sake, ProjA and ProjB. ProjB is set as the StartUp project. Furthermore, ProjA is configured to be built as a .dll file which I am to link with ProjB, which in turn is configured to be built as a .exe file. So far I've linked ProjA to ProjB by adding a dependency to ProjB via referencing (RMB>Add>Reference).
With all that out of the way, I wanted to test whether the setup worked as intended. So I did the following. In ProjA, I wrote the following Test code:
Test.h
#pragma once
namespace ProjA {
_declspec(dllexport) void Print();
}
Test.cpp
#include "Test.h"
#include <stdio.h>
namespace ProjA {
void Print()
{
printf("Testing\n");
}
}
And in ProjB:
TestEntry.cpp
#include <stdio.h>
namespace ProjA {
_declspec(dllimport) void Print();
}
void main()
{
ProjA::Print(); //breakpoint1
printf("Testing (2)\n"); //breakpoint2
}
I set two breakpoints at the commented lines for debugging, built the projects and ran the program. At breakpoint1, I expected the console to print "Testing" and proceed to the next line, followed by breakpoint2 printing "Testing (2)" and proceeding to the next line before exiting. However, the console was blank at breakpoint1. It only printed the line from the main function at breakpoint2.
So what am I doing wrong here? Is there something wrong with the way I included the projects? Was there a linking procedure I skipped? Is there a specific way I need to set up Visual Studios 2017 in order for it to work? Are there other plugins/APIs I should be installing?
To test it out further, I commented out everything in ProjA. This time I expected a linking error when building the projects, but everything ran in the exact same manner. This confused me even more. It's as if I didn't even link the projects and the main function is not identifying the Test codes via the include. That's the only thing I could deduce so far.
As suggested by SoronelHaetir, I redid the whole thing using #defines and namespaces. It's working as intended now. In conclusion, the crude method was a bad idea from the getgo.

Where to put implementation when using Doctest alongside code

I'm using doctest for the tests in my C++ project.
I would like to put the test code alongside my implementations, as the library says is possible, but I can't seem to figure out what to do with the doctest implementation code.
I have a doctest.cpp file that looks like this:
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
A main.cpp that looks like this:
#include "thing.h"
int main(int argc, const char *argv[]) {
do_thing();
}
thing.h is self-explanatory, but thing.cpp looks like this:
do_thing() {}
TEST_CASE("Test thing") {
CHECK(1 == 1);
}
CMake is used to create two executables, a Tests executable and a Main executable.
If I don't include doctest.cpp in my project sources for Main, I get undefined reference errors because it can't find a definition for all the testing stuff in doctest.
However, if I do include it I get errors because there are multiple main() functions in one target.
I can't find any info on this in the doctest documentation.
How are you meant to get around this?
The author of the library gave a good response in this issue:
DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN implements the test runner and also defines a main() function.
DOCTEST_CONFIG_IMPLEMENT implements ONLY the test runner.
If you define your own main() then you should use DOCTEST_CONFIG_IMPLEMENT - have a look at the relevant docs.
You will need the test runner implemented in your main executable (that means doctest.cpp) since you are writing your tests alongside your production code.
You can also define DOCTEST_CONFIG_DISABLE when building the main executable so tests are written in the production code but aren't compiled (you will still need doctest.cpp so it all links). This way you won't need to #ifdef the tests.
You could also entirely remove the test executable and use the main executable for running the tests - docs.
I went with the first option of writing my own main() function.
I encountered the same problem and one workaround is to add -DDOCTEST_CONFIG_DISABLE to the compiler flags when you compile Main.

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);
}

C++ Unit Test in Visual Studio 2012

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.