Howto check for existence of test in sub directory?

Hi all,

I need to check if a test with a certain name already exists.

The obvious way is the following:

add_test(NAME my_test COMMAND /bin/true)

if(TEST my_test)
    message(STATUS "yes")
else()
   message(STATUS "no")
endif()

This works for tests added in the same directory, but not if the test was added in a sub directory. I have tested it with CMake 3.22 and CMake 3.23.

Please see this github repo where I set up an example.

Shouldn’t test names be global? How can I check for tests defined in any directory?

Regards
Georg

It doesn’t look like test names are global at all. I really don’t know what happens if you have overlaps though…

Looks like it’s fine to have the same test name in different directories:

Test project …/cm-test-collide/build
    Start 1: duplicate
1/3 Test #1: duplicate ........................   Passed    0.00 sec
    Start 2: duplicate
2/3 Test #2: duplicate ........................   Passed    0.00 sec
    Start 3: duplicate
3/3 Test #3: duplicate ........................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 3

In the meanwhile I managed to solve my original problem in another way, so I don’t need to check for the existence of tests anymore.

Thanks for having at look at it, Ben. The behavior seems a bit strange at first as I

  • didn’t expect multiple tests with the same name to be valid and
  • did expect that if(TEST ...) works globally or at least catches the same tests as ctest would do on a directory level (tests defined in the current level and below)

Perhaps the documentation could be improved at that point.

However for me its solved, as I don’t need it anymore.

1 Like