如何在 2012 年在 Linux 上设置 googletest?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8739495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to setup googletest on Linux in the year 2012?
提问by Kit Ho
I am using Linux machine. I have download the googletest package from here
我正在使用 Linux 机器。我已经从这里下载了 googletest 包
However, there is no installation guide or other blogs related on how to set it up properly The README file is no good that I can't understand what it is talking about?
但是,没有安装指南或其他有关如何正确设置的博客 README 文件不好,我无法理解它在说什么?
Can anyone provide a simple example on how to test a simple function inside a .cc file with that gtest package?
任何人都可以提供一个简单的示例,说明如何使用该 gtest 包测试 .cc 文件中的简单函数?
回答by Darren Burgess
These instructions get the testing framework working for the Debug configuration.
这些说明使测试框架适用于调试配置。
Get Google C++ Testing Framework
获取 Google C++ 测试框架
1.Download the latest gtest framework
1.下载最新的gtest框架
2.Unzip to C:\gtest
2.解压到 C:\gtest
Build the Framework Libraries
构建框架库
1.Open C:\gtest\msvc\gtest.sln
in Visual Studio
1.C:\gtest\msvc\gtest.sln
在Visual Studio中打开
2.Set Configuration to "Debug"
2.将配置设置为“调试”
3.Build Solution
3.构建解决方案
Create and Configure Your Test Project
创建和配置您的测试项目
1.Create a new solution and choose the template Visual C++ > Win32 > Win32 Console Application
1.新建解决方案,选择模板Visual C++ > Win32 > Win32 Console Application
2.Right click the newly created project and choose Properties
2.右键单击新创建的项目并选择属性
3.Change Configuration to Debug.
3.将配置更改为调试。
4.Configuration Properties > C/C++ > General > Additional Include Directories: Add C:\gtest\include
4.Configuration Properties > C/C++ > General > Additional Include Directories: 添加 C:\gtest\include
5.Configuration Properties > C/C++ > Code Generation > Runtime Library: If your code links to a runtime DLL, choose Multi-threaded Debug DLL (/MDd). If not, choose Multi-threaded Debug (/MTd).
5.Configuration Properties > C/C++ > Code Generation > Runtime Library:如果您的代码链接到运行时 DLL,请选择多线程调试 DLL (/MDd)。如果没有,请选择多线程调试 (/MTd)。
6.Configuration Properties > Linker > General > Additional Library Directories: Add C:\gtest\msvc\gtest\Debug
6.配置属性>链接器>常规>附加库目录:添加 C:\gtest\msvc\gtest\Debug
7.Configuration Properties > Linker > Input > Additional Dependencies: Add gtestd.lib
7.Configuration Properties > Linker > Input > Additional Dependencies:添加 gtestd.lib
Verifying Everything Works
验证一切正常
1.Open the cpp in your Test Project containing the main()
function.
1.在包含该main()
函数的测试项目中打开 cpp 。
2.Paste the following code:
2.粘贴以下代码:
#include "stdafx.h"
#include <iostream>
#include "gtest/gtest.h"
TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::getchar(); // keep console window open until Return keystroke
}
1.Debug > Start Debugging
1.调试>开始调试
If this works you should see the console window open with your test results.
如果这有效,您应该看到控制台窗口打开并显示您的测试结果。
回答by James C
Here's what I did and you can adjust as necessary. I downloaded gtest-1.6.0.zip (from the releasespage) on my Linux box into ~/Downloads which typed out fully is /home/me/Downloads/
这是我所做的,您可以根据需要进行调整。我在我的 Linux机器上将 gtest-1.6.0.zip(从发布页面)下载到 ~/Downloads 中,完整输入的是 /home/me/Downloads/
Unzip the contents of gtest-1.6.0.zip into ~/Downloads/gtest-1.6.0/
将 gtest-1.6.0.zip 的内容解压到 ~/Downloads/gtest-1.6.0/
cd /home/me/Downloads
unzip gtest-1.6.0.zip
Build the gtest library because it's something you need to "include" in your test executable. Compile the object file gtest-all.o:
构建 gtest 库,因为它是您需要在测试可执行文件中“包含”的内容。编译目标文件 gtest-all.o:
g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc
Then build the library archive libgtest.a:
然后构建库存档 libgtest.a:
ar -rv libgtest.a gtest-all.o
Now you can create your test.cc file in ~/Downloads. Here is an example test file that I used to make sure it compiles.
现在您可以在 ~/Downloads 中创建您的 test.cc 文件。这是我用来确保它编译的示例测试文件。
#include "gtest/gtest.h"
TEST(blahTest, blah1) {
EXPECT_EQ(1, 1);
}
int main (int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
int returnValue;
//Do whatever setup here you will need for your tests here
//
//
returnValue = RUN_ALL_TESTS();
//Do Your teardown here if required
//
//
return returnValue;
}
To compile your own test and run it:
要编译您自己的测试并运行它:
g++ -I/home/me/Downloads/gtest-1.6.0/include -pthread test.cc libgtest.a -o test_executable
Then to execute it:
然后执行它:
./test_executable
And it should run fine. Modify as necessary from there.
它应该运行良好。从那里根据需要进行修改。
回答by levelont
An addendum to James C's answer:
James C 的回答的附录:
Note that building the library using gtest-1.6.0/src/gtest-all.cc
will require you to provide with a main method yourself. If you want to avoid that altogether and use the default implementation of the main method provided by Googletest, build your library including gtest_main.cc
.
请注意,使用 using 构建库gtest-1.6.0/src/gtest-all.cc
将需要您自己提供一个主要方法。如果您想完全避免这种情况并使用 Googletest 提供的 main 方法的默认实现,请构建您的库,包括gtest_main.cc
.
That is:
那是:
g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc gtest-1.6.0/src/gtest_main.cc
^^^^^^^^^^^^^^
ar -rv libgtest_main.a gtest_main.o gtest-all.o
^^^^^^^^^^^^
Also, keep in mind that implementing your own main method is notthe recommended way of defining the SetUp
and TearDown
behaviours; you should be using fixtures instead. Check the Googletest documentation on the topic.
另外,请记住,实现您自己的 main 方法不是定义SetUp
和TearDown
行为的推荐方式;你应该改用固定装置。查看有关该主题的Googletest 文档。