Support for test fixtures was added in CMake 3.7 and they do exactly what you want. Your particular scenario would be implemented like this:
set_property(TEST testX PROPERTY FIXTURES_REQUIRED Foo)
set_property(TEST testY PROPERTY FIXTURES_SETUP Foo)
set_property(TEST testZ PROPERTY FIXTURES_SETUP Foo)
You can then ask ctest
to run just testX
and it will automatically add testY
and testZ
to the set of tests to be executed:
ctest -R testX
It will also ensure that testX
will only run after testY
and testZ
have been passed. If either of testY
or testZ
fails, testX
will be skipped. New options -FS
, -FC
and -FA
were also added to ctest
in CMake 3.9 which allow automatic adding fixture setup/cleanup tests to be controlled at the ctest
command line. For example, to temporarily skip adding testY
to the test set but still automatically add testZ
, one could do this:
ctest -R testX -FS testY
The fixtures properties are described in the CMake docs and the following article covers the fixtures feature more comprehensively:
https://crascit.com/2016/10/18/test-fixtures-with-cmake-ctest/