- code:https://gitee.com/kacakaca/Test/tree/master/googletest_examples
- 执行:
cmake -S . -B build
cmake --build build/
执行完上述命令后,在./build/bin
下会有可执行文件,执行该文件 - 文件目录结构如下:
其中:
-
cmake相关文件有两个:
./CMakeLists.txt
和./example/src/CMakeLists.txt
-
CmakeLists.txt
cmake_minimum_required(VERSION 3.14) project(googletest_example) set(CMAKE_CXX_STANDARD 11) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) include(FetchContent) FetchContent_Declare( googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG release-1.10.0 ) FetchContent_MakeAvailable(googletest) enable_testing() include_directories( ${PROJECT_SOURCE_DIR}/example/include ) add_subdirectory(example/src) add_executable( basic_test example/test/people_test.cpp basic_test.cpp ) target_link_libraries( basic_test example gtest_main )
-
example/CmakeLists.txt
aux_source_directory(. DIR_EXAM_SRCS) add_library(example ${DIR_EXAM_SRCS})
-
-
源文件有:
/basic_test.cpp
,./example/include/people.hpp
,./example/src/people.cpp
和example/test/people_test.cpp
将basic_test.cpp和people_test.cpp分开了,这样可以增加其他的测试类-
basic_test.cpp
test的起始文件,通过RUN_ALL_TESTS()进行test的一些setup,并且执行链接的所有test,我仅写了最基础的,可以通过继承
::testing::EmptyTestEventListener
设置一些自己的配置,如:https://github.com/google/googletest/blob/master/googletest/samples/sample9_unittest.cc#include <gtest/gtest.h> #include <iostream> GTEST_API_ int main(int argc, char * argv[]) { std::cout << "begin test:" << std::endl; ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
-
example/include/people.hpp
需要测试的类的头文件, 只是简单的设置和获得人名#include <iostream> #include <string> class People { public: People(std::string name); ~People(); std::string getName(); private: std::string name_; };
-
example/src/people.cpp
需要测试的类的源文件#include "people.hpp" People::People(std::string name) { name_ = name; } People::~People() { } std::string People::getName() { return name_; }
-
example/test/people_test.cpp
测试文件,初始化的名字和得到的名字是否一致#include "people.hpp" #include <gtest/gtest.h> TEST(PeopleTest, Basic) { People a("li"); std::string name = a.getName(); EXPECT_EQ("li", name); }
-
参考:
google test quick start:https://google.github.io/googletest/quickstart-cmake.html
google test examples: https://google.github.io/googletest/samples.html
cmake help:https://cmake.org/cmake/help/latest/index.html
cmake不同目录:https://blog.csdn.net/ktigerhero3/article/details/70313350
为了不用每个测试类都写CMakeLists.txt:
- 需要创建测试文件夹(如example文件夹),并在此文件夹下增加include、src和test文件夹
- 将example下面的CMakeLists.txt删掉,
- 并且将主CMakeLists.txt改成如下:
cmake_minimum_required(VERSION 3.14) project(googletest_example) set(CMAKE_CXX_STANDARD 11) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) include(FetchContent) FetchContent_Declare( googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG release-1.10.0 ) FetchContent_MakeAvailable(googletest) enable_testing() file(GLOB_RECURSE TEST_LIST ${CMAKE_CURRENT_SOURCE_DIR}/*/test/*.cpp) foreach(file ${TEST_LIST}) # find parent folder of test string(REGEX REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/(.*)/test/(.*\.cpp$)" "\\1" name ${file}) message(STATUS "test-${name}: file: ${file}") set(TEST_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/${name}) include_directories(${TEST_FOLDER}/include) add_executable( ${name}_test ${file} basic_test.cpp ) file(GLOB_RECURSE TEST_LIB_SRCS ${TEST_FOLDER}/src/*.cpp) add_library(${name}_test_lib ${TEST_LIB_SRCS}) target_link_libraries( ${name}_test ${name}_test_lib gtest_main ) endforeach()