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.
CodePudding user response:
- 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();
}