I've found two ways to set this up: Either compile the whole GoogleTest framework directly into each of the test projects, or create a library project to hold it. Using a library will give faster build times, but you'll need to make sure that compile/link options are the same on the library and the test projects.
Option 1: Compiling GoogleTest Directly in the Test Project
- Create a new project from the Google Test template. Instructions here if needed.
- Uninstall the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet package.
- Install the latest gmock NuGet package from Google (currently v1.10.0).
- Add the file
gtest_main.cc
to the project. It should be in ..\packages\gmock.1.10.0\lib\native\src\gtest\src\
At this point the project should look something like this (if it doesn't, try Unloading and Reloading the project):
The final configuration step is to disable use of Precompiled Headers for the three Google .cc
files (Important: Notice the empty fields too).
Option 2: Using GoogleTest in a Static Library Project
- Create a new project from the Static Library (C++) template. Instructions here if needed.
- Delete all generated
.h
/.cpp
files (pch.h
, pch.cpp
, framework.h
, <ProjectName>.cpp
, etc)
- Install the latest gmock NuGet package from Google (currently v1.10.0).
- Disable use of Precompiled Headers for the library project (see related pic above).
- Create a new project from the Google Test template. Instructions here if needed.
- Uninstall the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet package.
- Add the file
gtest_main.cc
to the project. It should be in ..\packages\gmock.1.10.0\lib\native\src\gtest\src\
- Disable use of Precompiled Headers for
gtest_main.cc
(see related pic above).
- Add the library project to the test project's Project References.
- Add
..\packages\gmock.1.10.0\lib\native\include\
to the test project's Include Directories under VC++ Directories
The solution structure should now look something like this:
Writing the Tests
Either way, you are now ready to start writing tests using GoogleMock. Add #include "gmock/gmock.h"
to the pch.h
file:
//
// pch.h
// Header for standard system include files.
//
#pragma once
#include "gtest/gtest.h"
#include "gmock/gmock.h"
Open the generated Test.cpp
file and try it.
#include "pch.h"
class MockTest {
public:
MOCK_METHOD(void, SomeMethod, ());
};
TEST(TestCaseName, TestName) {
MockTest mock;
EXPECT_CALL(mock, SomeMethod);
mock.SomeMethod();
EXPECT_EQ(1, 1);
EXPECT_TRUE(true);
}
<ImportGroup Label="ExtensionTargets">
section it is vital that the googletest import line comes before gmock. These can get out of order when NuGet packages are removed and added. To fix this simply load up the vcxproj in a text editor and swap the lines. – Juetta