How to tell a test under cmake/ctest where to find its input data ... without changing hard-coded file names

add_test command accepts WORKING_DIRECTORY option. You can set this option to a directory where a test is located. So, the test will find its data file:

add_test(NAME ${test_name} COMMAND "${test_src}"
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

The variables ${CMAKE_CURRENT_SOURCE_DIR} and ${CMAKE_SOURCE_DIR} are helpful. The first one is the source directory to the current binary location. The latter is the root to the top level of the source tree.

Supposed you have an input file at prj/test/inputfile you could get the path to it with${CMAKE_CURRENT_SOURCE_DIR}/inputfile. You can pass the path as an argument to your test.

Tags:

Cmake

Ctest