2

I have some tests in my build that are optional. My CMakeLists.txt looks approximately like:

add_custom_target(all-tests)

add_executable(A ...)
add_dependencies(all-tests A)
add_test(NAME A COMMAND ...)

add_executable(B ...)
add_dependencies(all-tests B)
add_test(NAME B COMMAND ...)

## optional
add_executable(C EXCLUDE_FROM_ALL ...)
add_test(NAME C COMMAND ...)

The idea being that I can run

$ make all-tests
$ ctest

To both compile all my unit tests and then run them. The problem is, since C is optional, it doesn't build (all desired). And since it didn't build, it doesn't exist, and gets reported as having failed:

The following tests FAILED:
      6 - C (Not Run)
Errors while running CTest

Is there a way to ignore this failure / have CTest not try to run tests that don't exist / otherwise express the idea of an optional test? I have a class of these optional tests that have similar names, so it'd be nice to be able to use ctest -R to run them, if they are built, rather than having to configure some script or other approach.

Barry
  • 286,269
  • 29
  • 621
  • 977

1 Answers1

0

Make the optional tests depend on the existence of the built test executables by setting the REQUIRED_FILES property:

add_test(NAME C COMMAND ...)
add_executable(C EXCLUDE_FROM_ALL ...)
set_property(TEST C PROPERTY REQUIRED_FILES "$<TARGET_FILE:C>")

The use of the generator expression TARGET_FILE requires CMake 3.0 or later.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • Using `$` didn't work, I don't know if it even expands or not. I tried `${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/C` (which is definitely the correct path for this binary), but `ctest` still fails on the non-existing ones. – Barry Aug 06 '18 at 17:29
  • If it helps, I made a new question with an extremely reduced example [here](https://stackoverflow.com/q/51713184/2069064) – Barry Aug 06 '18 at 17:55
  • Okay - this seems to get ctest to make the test as "not run", but ctest itself will still consider those as failing for the purposes of its exit code. The motivation is still very much for ctest itself to succeed. – Barry Aug 06 '18 at 19:50