-2

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.

273K
  • 29,503
  • 10
  • 41
  • 64
Talset
  • 61
  • 2
  • 18
  • When you run it in the debugger, does it show the stack trace? – 273K Jul 12 '22 at 15:10
  • I guess this is moot since you already determined the problem, but no, I can only see "Exception thrown at 0x00007FFC29D05C87 in testhost.exe: 0xC0000005: Access violation executing location 0x00007FFC29D05C87." – Talset Jul 12 '22 at 15:47

1 Answers1

1
  1. You mix the very different test frameworks.
  2. The more important thing is that you do not initialize Google test framework.
  3. 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();
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • Thank you, your example runs without crashing (after adding the using ::testing::Return statement, adding the opening curly brace, and changing the configuration type to .exe). – Talset Jul 12 '22 at 15:43
  • @Talset Thank you for letting me know about syntax errors. The mobile screen is too small for seeing the code in whole. – 273K Jul 12 '22 at 15:46