Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run ctest from different directory than build directory used by cmake?

Tags:

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

like image 503
user1357687 Avatar asked Jul 28 '16 19:07

user1357687


People also ask

Is CTest part of CMake?

CTest is the part of CMake that handles testing your code. CTest allows for an easy way to run your newly built programs with various argument and option settings, and then check the results against expected output.

What is CTest in CMake?

Description. The ctest executable is the CMake test driver program. CMake-generated build trees created for projects that use the enable_testing() and add_test() commands have testing support. This program will run the tests and report results.

What does CMake add_test do?

Options To add_test(...) COMMAND specifies the command to run for the test. Essentially, this can be any command that would normally run in your terminal. You can also pass the name of an executable target, and CMake will replace it with the full location of the executable.

Does CMake facilitate unit tests?

¶ CMake facilitates testing your software through special testing commands and the CTest executable. First, we will discuss the key testing commands in CMake. To add testing to a CMake-based project, simply include(CTest) and use the add_test command.


1 Answers

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) 
like image 99
phcerdan Avatar answered Sep 19 '22 15:09

phcerdan