CMake: How to specify directory where ctest should look for executables?

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.


Documentation for add_test specifies WORKING_DIRECTORY option for long form of the command. Value of this option is used as a directory in which 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 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.