如何设置 Google C++ 使用 Visual Studio 2005 的测试框架 (gtest)
它没有在网站上记录,人们似乎在设置框架时遇到问题。 有人可以展示示例项目设置的分步介绍吗?
It is not documented on the web site and people seem to be having problems setting up the framework. Can someone please show a step-by-step introduction for a sample project setup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
(这些说明使测试框架适用于调试配置。将相同的过程应用于发布配置应该非常简单。)
获取 Google C++ 测试框架
C:\gtest
构建框架库< /strong>
C:\gtest\msvc\gtest.sln
创建并配置您的测试项目
C:\gtest\include
C:\gtest\msvc\gtest\Debug
或C:\gtest\msvc\gtest-md\Debug
,具体取决于 gtestd 的位置。 libgtestd.lib
验证一切正常
main()
函数的 cpp。粘贴以下代码:
<前><代码>#include“stdafx.h”;
#include
#include“gtest/gtest.h”
测试(样本测试用例,样本测试)
{
EXPECT_EQ(1, 1);
}
int main(int argc, char** argv)
{
测试::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::getchar(); // 保持控制台窗口打开直到按下 Return 键
}
调试> 开始调试
如果一切正常,您应该会看到控制台窗口出现并显示单元测试结果。
(These instructions get the testing framework working for the Debug configuration. It should be pretty trivial to apply the same process to the Release configuration.)
Get Google C++ Testing Framework
C:\gtest
Build the Framework Libraries
C:\gtest\msvc\gtest.sln
in Visual StudioCreate and Configure Your Test Project
C:\gtest\include
C:\gtest\msvc\gtest\Debug
orC:\gtest\msvc\gtest-md\Debug
, depending on the location of gtestd.libgtestd.lib
Verifying Everything Works
main()
function.Paste the following code:
Debug > Start Debugging
If everything worked, you should see the console window appear and show you the unit test results.
Arlaharen 所说的基本上是正确的,只是他遗漏了解释链接器错误的部分。 首先,您需要在不使用 CRT 作为运行时库的情况下构建应用程序。 无论如何,您应该始终这样做,因为它确实简化了应用程序的分发。 如果您不这样做,那么您的所有用户都需要安装 Visual C++ 运行时库,而那些不这样做的用户会抱怨他们的系统上缺少神秘的 DLL...静态地使用 CRT,您可以在以后的支持中省去很多麻烦(相信我——我已经通过惨痛的教训才学会了这一点!)。
无论如何,要执行此操作,您需要转到目标的属性 -> C/C++-> 代码生成-> 运行时库,对于您的发布版本,需要将其设置为“多线程”,对于您的调试版本,需要将其设置为“多线程调试”。
由于 gtest 库是以相同的方式构建的,因此您需要确保链接到正确版本的 it,否则链接器将引入运行时库的另一个副本,即您看到的错误(顺便说一句,无论您是否使用 MFC,这都不会产生影响)。 您需要将 gtest 构建为调试和发布模式并保留两个副本。 然后,您可以在发布版本中链接 gtest.lib/gtest_main.lib ,在调试版本中链接 gtestd.lib/gtest_maind.lib 。
另外,您需要确保您的应用程序指向存储 gtest 头文件的目录(在属性 -> C/C++ -> 常规 -> 其他包含目录中),但是如果出现链接器错误,我假设您已经设法使这部分正确,否则您将首先处理更多的编译器错误。
What Arlaharen said was basically right, except he left out the part which explains your linker errors. First of all, you need to build your application without the CRT as a runtime library. You should always do this anyways, as it really simplifies distribution of your application. If you don't do this, then all of your users need the Visual C++ Runtime Library installed, and those who do not will complain about mysterious DLL's missing on their system... for the extra few hundred kilobytes that it costs to link in the CRT statically, you save yourself a lot of headache later in support (trust me on this one -- I've learned it the hard way!).
Anyways, to do this, you go to the target's properties -> C/C++ -> Code Generation -> Runtime Library, and it needs to be set as "Multi-Threaded" for your Release build and "Multi-Threaded Debug" for your Debug build.
Since the gtest library is built in the same way, you need to make sure you are linking against the correct version of it, or else the linker will pull in another copy of the runtime library, which is the error you saw (btw, this shouldn't make a difference if you are using MFC or not). You need to build gtest as both a Debug and Release mode and keep both copies. You then link against gtest.lib/gtest_main.lib in your Release build and gtestd.lib/gtest_maind.lib in your Debug build.
Also, you need to make sure that your application points to the directory where the gtest header files are stored (in properties -> C/C++ -> General -> Additional Include Directories), but if you got to the linker error, I assume that you already managed to get this part correct, or else you'd have a lot more compiler errors to deal with first.
我制作了有关设置的视频教程:
http://www.youtube.com/watch?v=mzSzwQOmMRs
I did a video tutorial about the setup:
http://www.youtube.com/watch?v=mzSzwQOmMRs
构建完 gtest 后,这就是我所做的:
之后,我只需根据需要使用 TEST 或 TEST_F 编写测试,并将它们与我的 main 函数一起编译:
Having built gtest, this is what I have done:
After that I just write my tests using TEST or TEST_F as appropriate and compile them together with my main function:
如果您不想编写自己的 main() 进行测试,您可以使用 gtest_main.lib 中定义的 main() 函数,但随后您会在 VS2012 中收到链接器错误“必须定义入口点”。
在您的测试项目中,将 ProjectProperties->Linker->System->SubSystem 设置为“Console”,因为这将强制 VS2012 查找名为“main()”的入口点,并将在 gtest_main.lib 中找到它(前提是您已正确链接它)。
If you don t want to write your own main() for tests you can use the main() function defined in gtest_main.lib but then you get linker errors "Entry point must be defined" in VS2012.
In your test-project set ProjectProperties->Linker->System->SubSystem to "Console" as this will force VS2012 to look for an entry point called "main()" and will find it in gtest_main.lib (provided you ve linked it in properly).
在 Microsoft Visual Studio 中,错误配置的运行时库类型会导致链接错误。
VS 2005(和2008)默认使用多线程DLL或多线程调试DLL。
但 Google 测试库默认使用多线程或多线程调试运行时。
因此,为谷歌测试库选择合适的运行时库类型。 (在配置属性 -> 代码生成 -> 运行时库中)。
In Microsoft Visual Studio, misconfigured runtime library type causes link errors.
VS 2005(and 2008) uses Multithreaded DLL or Multithreaded Debug DLL as default.
But Google Test library uses Mulithreaded or Mulithreaded debug runtime as default.
So, choose appropriate run time library type for google test library. (in Configuration properties -> Code Generation -> Runtime Library).