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
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.
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.
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.
¶ 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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With