Both DocTest and Catch 2 not running unit test - c++

I'm starting a new win32 C++ project using Visual Studio 2019 (v. 16.0.4) and Resharper (v. 2019.1.1) and can't get either the Catch2 or Doctest unit testing framework to run a test. I prefer Doctest and used that first and when that didn't work, I tried Catch2 and had the same result. Both frameworks find the test, but give the status "Inconclusive Test not run". The Catch2 warning states: "2019.05.19 08:47:46.447 WARN Element CatchTest Test was left pending after its run completion.
". All the code below is for Catch2:
Engine.h
#pragma once
#include "pch.h";
#include <SDKDDKVer.h>;
int wWinMain( dv* ghInst, dv* ghPrevInst, dv* gupCmdLine, dsd gsdCmdShowFlag);
int test();
Engine.cpp
#include "pch.h"
//#include "..\DocTest_2_2_2.h"
#include "..\Catch_2_7_2.h"
#include "Engine.h"
int wWinMain( dv* ghInst, dv* ghPrevInst, dv* gupCmdLine, dsd gsdCmdShowFlag) {
return 0;
};
int test() { return 3; }
TEST_CASE("CatchTest Test") {
REQUIRE(test() == 3);
}
DocTest.cpp (Using Catch2 code, DocTest code is commented out)
#include "pch.h"
//#define DOCTEST_CONFIG_IMPLEMENT
//#include "..\DocTest_2_2_2.h"
#define CATCH_CONFIG_MAIN
#include "..\Catch_2_7_2.h"
I found that there was an issue like this on conversations about prior versions of Visual Studio and Resharper but I'm using the latest versions and still have a problem.
In Resharper options, I have "Enable Catch support" selected under "C++ Tests" and also have "Enable MSTest support" selected under "MsTest". I have the x64 architecture selected for both the unit tests and on all project configuration settings.
Any help with this is appreciated. Thank you.

Your wMinMain does not run the tests. See doctest docs on how to provide a correct main entry point.

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

Project structure issue

My goal is to achieve a test project on VisualStudio 2019.
I think that my main problem is with How to structure my project. Because, at the end, I find many files and don't know how to manage them.
These are the test .cpp files :
TIStream.cpp :
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/data/monomorphic.hpp>
#include "../ReemasCore/serialization.h"
#include <string.h>
BOOST_AUTO_TEST_SUITE(Serialization_InMemStream)
BOOST_AUTO_TEST_CASE(InMemStreamCtor_SizePositif_CorrectPointer)
{
auto in = std::make_unique<InMemStream>(10);
BOOST_TEST(in->ppStart != nullptr);
BOOST_TEST(in->ppCurrent == in->ppStart);
BOOST_TEST(in->ppEnd == in->ppStart + 10);
}
BOOST_AUTO_TEST_SUITE_END()
TTCPServer :
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(TCP_SERVER)
BOOST_AUTO_TEST_CASE(Server_Case1)
{
BOOST_TEST(1 == 1);
BOOST_TEST(true);
}
BOOST_AUTO_TEST_SUITE_END()
Based on this mailing the dynamic version of Boost Test doesn't contain the main function from version 1.34.1 and up. So if you want to use the shared version of Boost Test you need to follow the instructions from Boost Test doc.
You have to put #define BOOST_TEST_MODULE test module name before the #include <boost/test/unit_test.hpp>. As the documentation says it must be defined exactly for one compilation unit of your test module. As you create different executables for every test, then it practically means you have to put it into every test source file.

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

Using Catch2 in Visual C++ 2015

I'm trying to make a Unit Testing project using the Catch framework, but am faced with Link errors.
I've set up the project as follows:
Create a Native Unit Test project
Add Catch to the include directories
Add #include <catch.hpp> to stdafx.h
Write the following simple source file
unittest.cpp:
#include "stdafx.h"
namespace Catch2_Test
{
TEST_CASE("Y U no work")
{
REQUIRE(1);
}
}
For integrating Catch into Visual Studio
refer the ACCU article Integrating the Catch Test Framework into Visual Studio, by Malcolm Noyes
For using Catch2 in pre-compiled headers,
refer Catch2 issue 1061, where horenmar gave an example. The changes have been released as part of v2.1.0
In summary, the solution given is:
// stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#define CATCH_CONFIG_ALL_PARTS
#include "catch.hpp"
// PCH-test.cpp:
#include "stdafx.h"
#undef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED
#define CATCH_CONFIG_IMPL_ONLY
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
// tests1.cpp:
#include "stdafx.h"
TEST_CASE("FooBarBaz") {
REQUIRE(1 == 2);
}
The problem was that I had cloned Catch2 from the master branch, while the VS integration worked on a branch off Catch.

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.