29

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

Daniele
  • 2,672
  • 1
  • 14
  • 20
user1357687
  • 571
  • 1
  • 6
  • 9
  • 4
    You can tell [`ctest`](https://cmake.org/cmake/help/latest/manual/ctest.1.html#options) 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. – Kevin Feb 19 '21 at 16:07
  • @squareskittles Thanks, please submit that as an answer. – Max Barraclough Mar 30 '21 at 18:44

1 Answers1

35

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)
phcerdan
  • 730
  • 7
  • 16