c++ccmake

Is it possible in CMake to set properties of tests in a different directory scope than the one the test is defined in?


For more structured test execution with "ctest -L" I want to set some labels on my unit tests. The labels I want to assign to a test are not yet clear at the time I declare the test. CMake seems to fail finding the test when not in the same directory scope, however with the same semantics it can modify properties of a target.

Here is my setup:

.
├── CMakeLists.txt
└── a
    ├── CMakeLists.txt
    ├── a.cpp
    └── test_a.cpp

Where: ./CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(cmaketest LANGUAGES CXX)

enable_testing()

add_subdirectory(a)

# modify target and test properties
set_target_properties(a PROPERTIES LABELS "bla") # <-- works
set_tests_properties(mytest_a PROPERTIES LABELS "bla") # <-- fails

./a/CMakeLists.txt:

add_library(a src/a.cpp)

# add tests for my
add_executable(test_a test/test_a.cpp)
target_link_libraries(test_a a)

add_test(NAME mytest_a COMMAND ./test_a)

CMake output is:

-- The CXX compiler identification is GNU 9.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:9 (set_tests_properties):
   set_tests_properties Can not find test to add properties to: mytest_a
   
-- Configuring incomplete, errors occurred!

using set_tests_properties(mytest_a PROPERTIES LABELS "bla") in a/CMakeLists.txt works, but not in the parent scope. Is this behavior intended? if so why? Is there a workaround?


Solution

  • If you google ""set_property given TEST names that do not exist"", you should should be able to find (on discourse.cmake.org) Cannot set property of a test in a subdirectory. The TL;DR is that test names are not required to be unique across directory scopes (currently not documented- at least- not in the docs for add_test), so the behaviour you're observing is currently intended (both for set_tests_properties and set_property(TEST ...)). When asked, the maintainers were not aware of any workarounds, aside from writing a wrapper function that adds the property, and calling the function in the same directory scope as where the test is defined by add_test. An issue ticket was created in relation to this behaviour: https://gitlab.kitware.com/cmake/cmake/-/issues/22813.

    Another workaround could be to use a variable and then set the red property from the variable.