> 文章列表 > 【学习cmake-cookbook/chapter-04/recipe-03/cxx-example-3.5】

【学习cmake-cookbook/chapter-04/recipe-03/cxx-example-3.5】

【学习cmake-cookbook/chapter-04/recipe-03/cxx-example-3.5】

1、代码:cmake-cookbook/chapter-04/recipe-03/cxx-example-3.5 at master · qijitao/cmake-cookbook · GitHub

2、使用方法

(1)下载googletest代码

        

        其中fetch_git_repo是按如下定义的:

        

         

#include "sum_integers.hpp"
#include "gtest/gtest.h"#include <vector>int main(int argc, char **argv) {::testing::InitGoogleTest(&argc, argv);return RUN_ALL_TESTS();
}TEST(example, sum_zero) {auto integers = {1, -1, 2, -2, 3, -3};auto result = sum_integers(integers);ASSERT_EQ(result, 0);
}TEST(example, sum_five) {auto integers = {1, 2, 3, 4, 5};auto result = sum_integers(integers);ASSERT_EQ(result, 15);
}

上述代码中InitGoogleTest和RUN_ALL_TEST以及TEST是通过gtest/gtest.h 定义的,具体定义在googletest/src/gtest.cc中,由被gtest_main.cc包含,即gtest_main库(在googletest的CMakeLists.txt中定义)所以编译cpp_test时通过gtest_main库使用它们。

3、如何运行用例

(1)make test可以调用上述编译的cpp_test可执行文件,当做一个大用例执行。

 (2)直接调用编译生成的cpp_test可执行文件:

可以看到cpp_test内部执行的详细情况。