[cmake series] (4) test with Google test


Today, let’s talk about the cmake test.

However, we are still talking about C + + testing.

Cmake provides us with perfect testing support, for example, it has a special module ctest.

Cmake native test support

The cmake native support test is very simple, with only two functions:

add_test(NAME <name> COMMAND <command> [<arg>...]
         [CONFIGURATIONS <config>...]
         [WORKING_DIRECTORY <dir>]

This usage, in short, means that you need to implement an executable program that can accept input parameters, and use theadd_executableIt’s OK. Regardless of the directory where the executable program is stored, cmake will automatically fill it in for you.


add_executable(test_example test.cpp)
target_link_libraries(test_example example_lib)

add_test(NAME test_example1 COMMAND test_example --arg1=a --arg2=b)
add_test(NAME test_example2 COMMAND test_example --arg1=c --arg2=d)

Then, throughadd_testAfter registering your test cases, you are ready to run them in the following three ways after compiling.

  • make test
  • cmake --build . --target test
  • ctest

Of course, you can also use ctest in combination with cdash. Cdash is a place where you can record test logs. You can go to https://my.cdash.org/index.php Generally speaking, when the project is large, there will be demand in this area.


In addition to the ctest above, we also have a powerful Google test, which is a widely used C + + testing framework. Different from the above, Google test provides a test framework and mock.

Cmake also provides Google test support

gtest_add_tests(TARGET target
                [SOURCES src1...]
                [EXTRA_ARGS arg1...]
                [WORKING_DIRECTORY dir]
                [TEST_PREFIX prefix]
                [TEST_SUFFIX suffix]
                [TEST_LIST outVar]

It is used to replaceadd_testYes, by scanning the source code, it can read all the test cases, eliminating the problem of repeated writing on both sides, but it has a problem: once the test case changes, it needs to run cmake again, otherwise it can’t know the changed test case.

Therefore, cmake has provided a new method since 3.10

                     [EXTRA_ARGS arg1...]
                     [WORKING_DIRECTORY dir]
                     [TEST_PREFIX prefix]
                     [TEST_SUFFIX suffix]
                     [NO_PRETTY_TYPES] [NO_PRETTY_VALUES]
                     [PROPERTIES name1 value1...]
                     [TEST_LIST var]
                     [DISCOVERY_TIMEOUT seconds]

Compared withgtest_add_testsgtest_discover_testsIt achieves the purpose of registration by obtaining the test cases in the compiled executable program, so it is more robust. When the test cases change, there is no need to re run cmake (in fact, the principle is not magical, you use the compiled program runtime plus cmake)--gtest_list_testsParameters).

It’s also very easy to use. In the case of Google test dependency (I don’t know, I need to review the contents of the first two articles), through thefind_packageIntroduce dependencies.

find_package(GTest 1.10.0)

add_executable(test test.cpp)
target_link_libraries(test GTest::gtest GTest::gtest_main GTest::gmock

As for Google test itself, that is to write test cases by looking at the documents (if you are interested, you can leave a message, and I will write another one when I have the opportunity). In addition, I mentioned how to write unit tests in my previous tests in golang. In fact, the same is true here. Combined with the mock provided by Google test, we can write unit tests very easily.


  1. Testing With CTest

Thank you for reading. This article was first published in GitHub issues: https://github.com/xizhibei/b… (star and watch strongly suggest: P); in addition, this article can also be read in my blog: https://blog.xizhibei.me/2020… .

This article uses by-nc-sa to license.