How to Configure GoogleMock in Visual Studio 2017 After Already Installing GoogleTest?
Asked Answered
A

3

8

I installed the Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn package into my VS 2017 application solution. This was accomplished by adding a new GoogleTest project to my solution via "Add New Project/Other Languages/C++/Test/Google Test".

The testing works well, but now I am ready to try some mocking with gmock. So, I installed googlemock.v140.windesktop.static.rt-dyn via NuGet, but I have no idea of how to get it integrated into my test project.

My packages.config looks like this:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="googlemock.v140.windesktop.static.rt-dyn" version="1.7.0.1"  targetFramework="native" />
  <package id="Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn" version="1.8.0" targetFramework="native" />
</packages>

... but there are no external dependency header files or .lib files to link to as far as I can see. I don't know where to go from here. :-)

P.S. I have posted questions about GoogleTest on Microsoft's C++ forum, but they will not answer these types of questions about GoogleTest even though it was installed via Visual Studio.

Approximation answered 26/4, 2018 at 23:11 Comment(0)
W
8

There are some bad Google Test / Google Mock packages available on NuGet, such as the one referenced by this question. The one you want is the gmock package authored by Google Inc (version v1.8.1 as of this writing).

Once this package has been installed, your project's packages.config should look like:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="gmock" version="1.8.1" targetFramework="native" />
</packages>

And you can begin using GMock simply by adding

#include "gmock\gmock.h"

as mentioned in the documentation.

Watery answered 21/5, 2019 at 16:42 Comment(1)
I had to close & reopen the .sln for GMock to be found even though it showed in my packages.config file.Ramage
S
2

TLDR; The solution for me was renaming the include folder provided with Google Mock package

from:

packages\googlemock.v140.windesktop.static.rt-dyn.1.7.0.1\build\native\include\gtest

to

packages\googlemock.v140.windesktop.static.rt-dyn.1.7.0.1\build\native\include\gmock


I've the exact same situation here (same packages version), but I've solved in another way (since the solution from @R.Evans didn't work).

I've noticed that opening the Google Mock package there is a folder called gtest instead of gmock, and this name is hiding Google Test package. Uninstalling one, you can see the other one in the include path.

Moreover all Google Mock headers are using gmock as main path for their headers, so with the header folder called gtest Visual Studio is reporting tons of errors even in the Google Mock source code.

Slumgullion answered 1/2, 2019 at 7:24 Comment(1)
Have to do similar after #include "gtest/gmock.h", as gmock.h is using #include "gmock/gmock-actions.h". Some one publish the package without testing it.Wozniak
A
1

I stumbled upon an answer to my own question. After installing gmock via NuGet, I tried keying #include "gmock/gmock.h" in my test project's .cpp just under the #include "gtest/gtest.h". That did not work. I looked in the "External Dependencies" folder for any reference to gmock but did not find one. I was stumped until I replaced #include "gmock/gmock.h" with #include "gtest/gmock.h". There were no errors generated at that point. I looks like I am on my way to doing some google type mocks.

Approximation answered 27/4, 2018 at 3:22 Comment(1)
#include "gtest\gtest.h" #include "gtest\gmock.h" TEST(TestCaseName, TestName) { EXPECT_EQ(1, 1); EXPECT_TRUE(true); } int main(int argc, char** argv) { ::testing::InitGoogleMock(&argc, argv); }Rehabilitation

© 2022 - 2024 — McMap. All rights reserved.