The following code defines a test executable and two tests:
add_executable(foo_test ...)
add_test(NAME foo.test.1 COMMAND foo_test -1)
add_test(NAME foo.test.2 COMMAND foo_test -2)
With CMAKE_SKIP_TEST_ALL_DEPENDENCY
set to OFF
, running make test
will build the test executable before running the tests, unless EXCLUDE_FROM_ALL
is set as a target or directory property. Running ctest
directly will not build the test executable at all.
I’d like to discuss an alternative approach that would build the test executable before running the tests that rely on it, whether the tests are launched through ctest
or make test
and whether EXCLUDE_FROM_ALL
is set or not:
If the test command is an executable target, then (based on some variable or property that is yet to be determined) a test fixture is implicitly defined and added as a requirement of the test.
That means, the code
set(CMAKE_COME_UP_WITH_REASONABLE_NAME ON)
add_executable(foo_test ...)
add_test(NAME foo.test.1 COMMAND foo_test -1)
add_test(NAME foo.test.2 COMMAND foo_test -2)
would behave like
add_executable(foo_test ...)
add_test(NAME foo.test.1 COMMAND foo_test -1)
add_test(NAME foo.test.2 COMMAND foo_test -2)
add_test(NAME cmake_build.foo_test
COMMAND "${CMAKE_COMMAND}"
--build "${CMAKE_BINARY_DIR}"
--config $<CONFIGURATION>
--target foo_test
)
set_tests_properties(cmake_build.foo_test PROPERTIES
FIXTURES_SETUP cmake_build.foo_test
)
set_tests_properties(foo.test.1 PROPERTIES
FIXTURES_REQUIRED cmake_build.foo_test
)
set_tests_properties(foo.test.2 PROPERTIES
FIXTURES_REQUIRED cmake_build.foo_test
)