基础测试套件

# the usual boilerplate setup
cmake_minimum_required(2.8)
project(my_test_project
        LANGUAGES CXX)

# tell CMake to use CTest extension
enable_testing()

# create an executable, which instantiates a runner from
# GoogleTest, Boost.Test, QtTest or whatever framework you use
add_executable(my_test
               test_main.cpp)

# depending on the framework, you need to link to it
target_link_libraries(my_test
                      gtest_main)

# now register the executable with CTest
add_test(NAME my_test COMMAND my_test)

宏观 enable_testing() 做了很多魔术。首先,它创建了一个内置目标 test(用于 GNU make; RUN_TESTS 用于 VS),在运行时执行 CTest

add_test() 的调用最终会向 CTest 注册一个任意可执行文件,因此每当我们调用 test 目标时,可执行文件就会运行。

现在,像往常一样构建项目,最后运行测试目标

GNU Make 视觉工作室
make test cmake --build . --target RUN_TESTS