如何在Visual Studio中设置GoogleTest和GoogleMock? [英] How to set up both GoogleTest and GoogleMock in Visual Studio?
问题描述
使用Visual Studio 2017/2019,设置新的
With Visual Studio 2017/2019 it is really easy to set up a new Google Test project and start writing tests (as long as you don't mind using older versions of GoogleTest versions anyway).
但是也要使用GoogleMock吗?您可能会认为,由于Google早前将gtest/gmock组合在一起,所以这才行得通.只需#include "gmock/gmock.h"
并嘲笑.但是不可以, GoogleTest NuGet包根本不包含gmock
文件夹.
But what about using GoogleMock as well? You would think that since Google combined gtest/gmock some time ago that this would just work. Just #include "gmock/gmock.h"
and mock away. But no, the GoogleTest NuGet package that is automatically added by the template does not include the gmock
folder at all.
尝试添加第二个GoogleMock NuGet软件包会导致多个问题,例如gtest/gmock版本不匹配,包含路径重叠等.
Trying to add a second GoogleMock NuGet package causes multiple problems, such as mismatched gtest/gmock versions, overlapping include paths, etc.
用来自Google的一个替换Microsoft GoogleTest NuGet软件包会导致链接错误:
Replacing the Microsoft GoogleTest NuGet package with the one from Google causes a link error:
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
那么,在Visual Studio中设置GoogleTest/GoogleMock的当前推荐的方法(也是最不痛苦的方法)是什么?应该可以通过测试资源管理器"发现,运行和调试测试.
So what is the current recommended (and least painful) way to set up GoogleTest/GoogleMock in Visual Studio? Tests should be able to be discovered, run, and debugged via the Test Explorer.
推荐答案
我发现了两种设置方法:将整个GoogleTest框架直接编译到每个测试项目中,或者创建一个库项目来保存它.使用库可以缩短构建时间,但是您需要确保库和测试项目上的编译/链接选项相同.
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.
直接在测试项目中编译GoogleTest
- 通过 Google测试模板创建一个新项目.说明此处(如果需要).
- 卸载 Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet程序包.
- 从Google安装最新的 gmock NuGet软件包(当前为v1.10.0).
- 将文件
gtest_main.cc
添加到项目中.它应该在..\packages\gmock.1.10.0\lib\native\src\gtest\src\
中
- 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):
最后的配置步骤是对三个Google .cc
文件禁用 Precompiled标头(重要:也请注意空白字段).
The final configuration step is to disable use of Precompiled Headers for the three Google .cc
files (Important: Notice the empty fields too).
在静态库项目中使用GoogleTest
- 通过静态库(C ++)模板创建一个新项目.说明 gmock NuGet软件包(当前为v1.10.0).
- 对图书馆项目禁用预编译头(请参见上面的相关图片).
- 通过 Google测试模板创建一个新项目.说明此处(如果需要).
- 卸载 Microsoft.googletest.v140.windesktop.msvcstl.static.rt-static NuGet程序包.
- 将文件
gtest_main.cc
添加到项目中.它应该在..\packages\gmock.1.10.0\lib\native\src\gtest\src\
中
- 禁用
gtest_main.cc
的预编译头(请参见上面的相关图片). - 将库项目添加到测试项目的项目参考"中.
- 在 VC ++目录 下的测试项目的包含目录中添加
..\packages\gmock.1.10.0\lib\native\include\
- 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:
编写测试
无论哪种方式,您现在都可以开始使用GoogleMock编写测试了.将#include "gmock/gmock.h"
添加到pch.h
文件中:
Either way, you are now ready to start writing tests using GoogleMock. Add 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"
打开生成的Test.cpp
文件并尝试.
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);
}
这篇关于如何在Visual Studio中设置GoogleTest和GoogleMock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!