- code:https://gitee.com/kacakaca/Test/tree/master/googletest_examples
- Execution:
cmake -S . -B build
cmake --build build/
After executing the above command, there will be an executable file./build/bin
below , execute the file - The file directory structure is as follows:
in:
-
There are two cmake related files:
./CMakeLists.txt
and./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})
-
-
The source files are:
/basic_test.cpp
,./example/include/people.hpp
,./example/src/people.cpp
andexample/test/people_test.cpp
separate basic_test.cpp and people_test.cpp, so that other test classes can be added-
basic_test.cpp
The starting file of the test, some setup of the test through RUN_ALL_TESTS(), and execute all the tests linked, I only wrote the most basic, you can set
::testing::EmptyTestEventListener
some of your own configuration through inheritance, such as: 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
is the header file of the class that needs to be tested, just simply set and get the name of the person#include <iostream> #include <string> class People { public: People(std::string name); ~People(); std::string getName(); private: std::string name_; };
-
example/src/people.cpp
The source file of the class to be tested#include "people.hpp" People::People(std::string name) { name_ = name; } People::~People() { } std::string People::getName() { return name_; }
-
example/test/people_test.cpp
test file, whether the initialized name is consistent with the obtained name#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
In order not to write CMakeLists.txt for each test class:
- Need to create a test folder (such as the example folder), and add include, src and test folders under this folder
- Delete the CMakeLists.txt below the example,
- And change the main CMakeLists.txt as follows:
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()