Get list of CMake test names

CMake add_test() can be dynamically set by arbitrarily complex foreach(), if(), etc. logic. If it’s known that a subset of test names need parameters such as longer timeouts, these can be set dynamically after the tests are declared. A list of tests enabled is retrieved by the TESTS property. This list is useful to be queried for particular test names as in the example below.

This example supposes most tests are set to a short timeout, but a couple tests “test1” “test2” need a longer timeout. The variable “_test_names” will contain all the test names previously added in the CMakeLists.txt via “add_test()” in a list in the DIRECTORY scope.

get_property(_test_names DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY TESTS)

foreach(l test1 test2)
  if(${l} IN_LIST _test_names)
    set_tests_properties(${l} PROPERTIES TIMEOUT 600)
  endif()
endforeach()

NOTE:

  • get_property(_test_names GLOBAL PROPERTY TESTS) will return an empty list–DIRECTORY scope must be used.
  • The scope is strictly the DIRECTORY specified–tests added earlier in other directories will not be listed. Thus, this method must be applied in each directory where it’s desired to list targets.