Kit*_* Ho 20 c++ linux unit-testing googletest
我正在使用Linux机器.我从这里下载了googletest软件包
但是,没有关于如何正确设置的安装指南或其他博客自述文件是不行的,我无法理解它在说什么?
任何人都可以提供一个简单的例子来说明如何使用该gtest包测试.cc文件中的简单函数吗?
Jam*_*s C 14
这就是我所做的,你可以根据需要进行调整.我在我的Linux机器上下载了gtest-1.6.0.zip(从发布页面)到〜/下载完全输出的是/ home/me/Downloads /
将gtest-1.6.0.zip的内容解压缩到〜/ Downloads/gtest-1.6.0 /
cd /home/me/Downloads
unzip gtest-1.6.0.zip
Run Code Online (Sandbox Code Playgroud)
构建gtest库,因为它需要在测试可执行文件中"包含".编译目标文件gtest-all.o:
g++ -Igtest-1.6.0/include -Igtest-1.6.0 -c gtest-1.6.0/src/gtest-all.cc
Run Code Online (Sandbox Code Playgroud)
然后构建库存档libgtest.a:
ar -rv libgtest.a gtest-all.o
Run Code Online (Sandbox Code Playgroud)
现在,您可以在〜/ 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;
}
Run Code Online (Sandbox Code Playgroud)
编译自己的测试并运行它:
g++ -I/home/me/Downloads/gtest-1.6.0/include -pthread test.cc libgtest.a -o test_executable
Run Code Online (Sandbox Code Playgroud)
然后执行它:
./test_executable
Run Code Online (Sandbox Code Playgroud)
它应该运行正常.根据需要进行修改.
这些指令使测试框架适用于调试配置。
获取 Google C++ 测试框架
1.下载最新的gtest框架
2.解压到C:\gtest
构建框架库
1.C:\gtest\msvc\gtest.sln在Visual Studio中打开
2.将配置设置为“调试”
3.构建解决方案
创建并配置您的测试项目
1.创建一个新的解决方案并选择模板Visual C++ > Win32 > Win32 Console Application
2.右键单击新创建的项目并选择属性
3.将配置更改为调试。
4.配置属性 > C/C++ > 常规 > 其他包含目录:添加C:\gtest\include
5.配置属性 > C/C++ > 代码生成 > 运行时库:如果您的代码链接到运行时 DLL,请选择多线程调试 DLL (/MDd)。如果没有,请选择多线程调试 (/MTd)。
6.配置属性>链接器>常规>附加库目录:添加C:\gtest\msvc\gtest\Debug
7.配置属性>链接器>输入>附加依赖项:添加gtestd.lib
验证一切正常
1.打开测试项目中包含该函数的cpp main()。
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
}
Run Code Online (Sandbox Code Playgroud)
1.调试>开始调试
如果这有效,您应该会看到控制台窗口打开,其中包含您的测试结果。