Run ctest from different directory than build directory used by cmake?
Asked Answered
W

1

34

I would like to be able in a similar manner as I can run cmake like

cmake --build <bld_directory>

to run ctest like

ctest --build <bld_directory>

Obviously running ctest from the <bld-directory> will work, but it would be nice if I can just tell ctest where to look for its configuration file and where the test executables are located.

From the documentation it is not very clear (or I might not have looked in the right place) if this is possible at all or not.

It would great if somebody could shed some light on if this is possible or not ? Many thanks, Jiri

Wentzel answered 28/7, 2016 at 19:3 Comment(2)
You can tell ctest to look in a custom directory for the tests by specifying the command line option: ctest --test-dir /path/to/tests. Note, you must use CMake 3.20 or greater to get this CLI feature.Timothy
@squareskittles Thanks, please submit that as an answer.Commorant
R
40

Since CMake 3.20 ctest has the option --test-dir for exactly that.

--test-dir <dir> Specify the directory in which to look for tests.

For CMake older than 3.20:

I couldn't find the way to do it through ctest options, but it is doable using the rule make test which is linked to ctest.

In the Makefile generated by cmake in your build folder you can find the rule:

#Special rule for the target test
test:
    @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running tests..."
    /usr/bin/ctest --force-new-ctest-process $(ARGS)
.PHONY : test

make provides the option that you want with -C /path/to/build_directory/, and you can add any ctest options with ARGS='your ctest options here'

For example, from any directory in your system you can write:

make test -C /path/to/build_folder ARGS='-R SpecificTestIWantToRun -VV'

or

cmake --build <bld_directory> --target test -- ARGS="<ctest_args>"

Another approach without make, is to use parenthesis in your terminal to create a subshell. This will execute the command without changing the folder of your current shell.

(cd $build_folder; ctest -V)
Rouble answered 10/8, 2016 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.