Notification texts go here Contact Us Buy Now!

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

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.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.