cmake-gtest单元测试
cmake-gtest单元测试
前篇
- 官方
- clion 官方 gtest-config - https://www.jetbrains.com/help/clion/creating-google-test-run-debug-configuration-for-test.html#gtest-config
- Google Test(gtest)使用 - http://notes.maxwi.com/2017/11/29/gtest/
集成 gtest
- CLion下的gtest测试 - https://blog.csdn.net/weixin_38988633/article/details/92801084
这里使用的 clion 编辑器
TEST
在项目中添加一个 子模块 引入 gtest.
CMakeLists.txt 中加入 gtest. 附: 测试代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22# 测试 gtest 的代码
file(GLOB_RECURSE SRC_GTest
GTest/add/*.h
GTest/add/*.cpp
)
#set(mainCpp main.cpp)
set(mainCpp GTest/main.cpp) # gtest 入口文件
set(SOURCE_FILES ${mainCpp} ${SRC_OtherTest} ${SRC_ExternTest} ${AlgorithmTest} ${SRC_GTest})
############# googletest module begin #############
set(gtName gtest) # gtest 库名
add_subdirectory(googletest) # gtest 目录
include_directories(googletest/googletest/include) # gtest 头文件目录
link_directories(
${CMAKE_CURRENT_SOURCE_DIR}/googletest
)
############# googletest module end #############
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${gtName}) # 链接 gtestdone.
测试所有用例. 直接 build and run.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28// ----------- build -----------
"D:\CLion 2019.3.3\bin\cmake\win\bin\cmake.exe" --build E:\ws_cpp\CppLab\cmake-build-debug --target CppLab -- -j 6
[ 3%] Built target gtest
[100%] Built target CppLab
Build finished
// ----------- run -----------
E:\ws_cpp\CppLab\cmake-build-debug\CppLab.exe
[==========] Running 3 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 3 tests from TestCase
[ RUN ] TestCase.test1
---- test1 hello world
[ OK ] TestCase.test1 (0 ms)
[ RUN ] TestCase.test2
---- test2 hello world
[ OK ] TestCase.test2 (1 ms)
[ RUN ] TestCase.test3
---- test3 hello world
[ OK ] TestCase.test3 (2 ms)
[----------] 3 tests from TestCase (15 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test suite ran. (31 ms total)
[ PASSED ] 3 tests.
Process finished with exit code 0测试单个用例, 直接在 用例 上 右键 -> Run TestCase.xxx
TEST_F
添加一个类
GTestDemo
继承::testing::Test
, 这个GTestDemo
就是一个 test suite.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37// ----------- demotest.cpp -----------
class GTestDemo : public ::testing::Test {
private:
int mNum1 = 123; // 测试框架访问不到
public:
int mNum2 = 456;
GTestDemo() : Test() {
std::cout << "------ constructor" << std::endl;
}
~GTestDemo() {
std::cout << "--- deconstructor" << std::endl;
}
virtual void SetUp() {
Test::SetUp();
std::cout << "--- SetUp" << std::endl;
}
virtual void TearDown() {
Test::TearDown();
std::cout << "--- TearDown" << std::endl;
}
};
TEST_F(GTestDemo, tc_example_01) {
std::cout << "tc_example_01: " << mNum2 << std::endl;
}
TEST_F(GTestDemo, tc_example_02) {
std::cout << "tc_example_02" << std::endl;
}done. 只测试这个类的 测试用例
1
2
3
4
5
6
7
8
9
10
11
12
13E:\ws_cpp\CppLab\cmake-build-debug\CppLab.exe --gtest_color=no
Running 2 tests from 1 test suite.------ constructor
--- SetUp
tc_example_01: 456
--- TearDown
--- deconstructor
------ constructor
--- SetUp
tc_example_02
--- TearDown
--- deconstructor
2 tests from 1 test suite ran. (0 ms total)Process finished with exit code 0
clion 添加 单元测试 配置
- 官方 gtest-config - https://www.jetbrains.com/help/clion/creating-google-test-run-debug-configuration-for-test.html#gtest-config
测试所有用例
添加一个测试配置. 使用 suite/test 模式, suite 留空代表测试所有 test suite
- program arguments : 这里可以加入 可选参数, 指定测试某些用例
跑一下.
哪个 过了/没过 一目了然.
带信息的 assert
往后面加上 << "xxx"
即可
1 | ASSERT_TRUE(false) << "--- err: wolegequ"; // 将中断执行 |
指定测试用例
参数: --gtest_filter=*.*
, 匹配符合条件的用例.
测试用例
1
2
3
4TEST(TestCase, test3) {
std::cout << "---- test3 hello world" << std::endl;
EXPECT_EQ(3, add(1, 2));
}指定测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14$ .\CppLab.exe --gtest_filter=TestCase.test3 # 只测试 TestCase.test3 这个用例
Active code page: 65001
Note: Google Test filter = TestCase.test3
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestCase
[ RUN ] TestCase.test3
---- test3 hello world
[ OK ] TestCase.test3 (0 ms)
[----------] 1 test from TestCase (10 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (44 ms total)
[ PASSED ] 1 test.
可选参数
可以在可执行程序后面加入一些参数. 例如:
1 | $ .\CppLab.exe --gtest_filter=CBaseTest.test_* # 只测试匹配 CBaseTest.test_* 的用例, CBaseTest 一个test suit |
所有可选参数:
1 | This program contains tests written using Google Test. You can use the |
测试代码
1 | // ----------- add.h ----------- |