googletest 教程¶
1 编译安装¶
- 执行以下指令下载安装
git clone https://github.com/google/googletest.git
cd googletest # 仓库代码主目录.
mkdir build # Create a directory to hold the build output.
cd build
cmake .. # 只生成GoogleTest不生成GoogleMock
::: warning
cd googletest
是进入主目录下,而不是 googletest/googletest 目录
:::
- 在 windows 下 build 目录会生成.sln 文件,使用 visual studio 打开
- 生成 gtest、gtest_main 项目的静态库
2 googletest 的使用¶
上面的编译安装默认会生成 googletest 的静态库 lib。
1. 包含 lib 库
1. 使用 #pragma comment
添加 lib 库
2. 或在 vs 上配置 lib 库路径和 lib 名
2. 包含头文件路径:将 googletest/googletest/include 添加到 vs 包含目录中
3. 包含头文件 gtest.h:在使用 googletest 的地方首先包含 gtest.h 文件
#include "gtest/gtest.h"
#ifdef _DEBUG
#pragma comment(lib,"gtest_maind.lib")
#pragma comment(lib,"gtestd.lib")
#else
#pragma comment(lib,"gtest_main.lib")
#pragma comment(lib,"gtest.lib")
#endif
TEST(TestCaseName, TestName) {
EXPECT_EQ(1, 1);
EXPECT_TRUE(true);
}
//int main(int argc, char* argv[]) {
// testing::InitGoogleTest(&argc, argv);
// return RUN_ALL_TESTS();
//}
输出结果
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestCaseName
[ RUN ] TestCaseName.TestName
[ OK ] TestCaseName.TestName (0 ms)
[----------] 1 test from TestCaseName (1 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (3 ms total)
[ PASSED ] 1 test.
注意:在我们的程序中不需要提供 mian 函数,googletest 会提供,当然我们也可以自己提供 - 自定义 main 函数,需要以下内容
#include "gtest/gtest.h"
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
2.1 TEXT 和 TEXT_F¶
googletest 中有 2 类测试方式,基本测试 TEXT 和测试夹具 TEXT_F,TEXT_F 是用来复用同一测试数据,避免冗余的。具体使用可参考官方文档 - https://github.com/google/googletest/blob/master/docs/primer.md
2.2 Environment 类¶
Environment
用作测试环境生成释放,如初始化,清理操作放入其中。
1. 定义子类继承 Environment 类,实现 SetUp、TearDown 函数
1. SetUp
: 构造时调用
2. TearDown
: 析构时调用
2. 在 main 函数中使用 AddGlobalTestEnvironment 函数
class TestEnvironment : public ::testing::Environment {
public:
void SetUp() override { TestNcsf::init(); }
void TearDown() override { TestNcsf::clean(); }
};
int main(int argc, char** argv) {
::testing::AddGlobalTestEnvironment(new gtest_ncsf::TestEnvironment);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
3 命令行参数的使用¶
3.1 显示测试用例¶
- 参数:
--gtest_list_tests
- 示例:
./程序名 --gtest_list_tests
此命令参数只显示测试用例,不会执行测试用例
3.2 过滤测试用例¶
- 参数:
--gtest_filter
:
: 连接多个匹配项-
: 匹配项取反*
: 匹配多个字符?
: 匹配单个字符- 示例:
./程序名 --gtest_filter=测试用例名(TestSuiteName.TestName)
- 更多使用参数解释:
./foo_test
Has no flag, and thus runs all its tests../foo_test --gtest_filter=*
Also runs everything, due to the single match-everything * value../foo_test --gtest_filter=FooTest.*
Runs everything in test suite FooTest ../foo_test --gtest_filter=*Null*:*Constructor*
Runs any test whose full name contains either "Null" or "Constructor" ../foo_test --gtest_filter=-*DeathTest.*
Runs all non-death tests../foo_test --gtest_filter=FooTest.*-FooTest.Bar
Runs everything in test suite FooTest except FooTest.Bar../foo_test --gtest_filter=FooTest.*:BarTest.*-FooTest.Bar:BarTest.Foo
Runs everything in test suite FooTest except FooTest.Bar and everything in test suite BarTest except BarTest.Foo.
3.3 重复执行测试¶
- 参数:
--gtest-repeat
- 示例
$ foo_test --gtest_repeat=1000
Repeat foo_test 1000 times and don't stop at failures.
$ foo_test --gtest_repeat=-1
A negative count means repeating forever.
$ foo_test --gtest_repeat=1000 --gtest_break_on_failure
Repeat foo_test 1000 times, stopping at the first failure. This
is especially useful when running under a debugger: when the test
fails, it will drop into the debugger and you can then inspect
variables and stacks.
$ foo_test --gtest_repeat=1000 --gtest_filter=FooBar.*
Repeat the tests whose name matches the filter 1000 times.