In our projects, we always specify the path when we call add_test()
, e.g.:
add_test( ${filename} ${CMAKE_CURRENT_BINARY_DIR}/${filename} )
The documentation for add_test()
specifies the WORKING_DIRECTORY
option for the long form of the command. The value of this option is used as a directory in which the test operates:
add_test(NAME test_exe COMMAND test_exe WORKING_DIRECTORY ${UNIT_TEST_BIN_OUTPUT_DIR})
If you just want the test to find the executable, it is sufficient to use:
add_test(NAME test_exe COMMAND test_exe)
This is a long form of the add_test()
command. In this form, CMake checks whether COMMAND
is a target name, and, if it is so, replaces it with an absolute path to the executable corresponded to that target. Such way the test can be run from any directory.
Note, that automatic replacement of the target doesn't work for a short form of add_test()
which you use.
Using CMake 3.20 and greater, you can tell CTest which directory contains your tests by using a CLI option:
ctest --test-dir /path/to/your/tests
This is a less-invasive solution for existing tests, for which you don't want to modify the CMake files.