'Can ctest use relative path to run test?

Can ctest use relative path to run test? There is no problem to run ctest after cmake. But if I copy the whole folder to somewhere else, ctest cannot run properly. It is using absolute path and cannot find the binary.



Solution 1:[1]

--build-exe-dir

Specify the directory for the executable.

From https://cmake.org/cmake/help/v3.23/manual/ctest.1.html

Solution 2:[2]

I am currently facing the same issue. This answer is a work in progess answer:

  • when building your tests, when you call add_test, make sure that you use relative path, with an ugly trick like [1]
  • after the build, you must update DartConfiguration.tcl, like in [2]

[1]

get_target_property(_OUTPUT_DIRECTORY ${__TEST_EXE}
                    RUNTIME_OUTPUT_DIRECTORY)
if(${_OUTPUT_DIRECTORY} STREQUAL "_OUTPUT_DIRECTORY-NOTFOUND")
    set(_OUTPUT_DIRECTORY "./")
endif()
set(__ABS_TEST_EXE
    ${_OUTPUT_DIRECTORY}/$<TARGET_FILE_NAME:${__TEST_EXE}>)
string(REPLACE ${CMAKE_CURRENT_BINARY_DIR}/ "./" __TEST_EXE_PATH
       ${__ABS_TEST_EXE})
add_test(NAME ${_TEST_NAME} COMMAND ${__TEST_EXE_PATH})

[2]

- BuildDirectory: /absolute/path/to/you/build
+ BuildDirectory: ./

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zhaoyanggh
Solution 2 Jérôme