-V,--verbose = Enable verbose output from tests.
-VV,--extra-verbose = Enable more verbose output from tests.
With --output-on-failure
, CTest will print to screen the output of
failing tests.
You can select subsets of test to run:
By name, with the -R <regex>
flag. Any test whose name can be
captured by the passed regex will be run. The -RE <regex>
option
excludes tests by name using a regex.
By label, with the -L <regex>
flag. Any test whose labels can be
captured by the passed regex will be run. The -LE <regex>
option
excludes tests by label using a regex.
By number, with the -I [Start,End,Stride,test#,test#|Test file]
flag.
This is usually not the most convenient option for selecting subsets of
tests.
It is possible to rerun failed tests with:
$ ctest --rerun-failed
Finally, you can parallelize test execution:
$ ctest -j N
$ ctest --parallel N
Build the “summing up” example from above.
Run the cpp_test
binary directly (it will produce no output).
Run ctest --verbose
.
Try to break the code and check whether CTest will detect the degradation.
Try to add a second test to the project.
Exercise: running tests in parallel and understanding the COST property
This example is in content/examples/testing-parallel/
.
Build the project and run the test set with ctest
, observe the order of tests.
Now uncomment the lines containing COST in CMakeLists.txt
:
# set minimum cmake version
cmake_minimum_required(VERSION 3.14)
# project name
project(example LANGUAGES NONE)
# detect python
find_package(Python REQUIRED)
# define tests
enable_testing()
add_test(a ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/a.py)
add_test(b ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/b.py)
add_test(c ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/c.py)
add_test(d ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/d.py)
#set_tests_properties(a b c d PROPERTIES COST 0.5)
add_test(e ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/e.py)
add_test(f ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/f.py)
add_test(g ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/g.py)
#set_tests_properties(e f g PROPERTIES COST 1.5)
add_test(h ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/h.py)
#set_tests_properties(h PROPERTIES COST 2.5)
add_test(i ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/i.py)
#set_tests_properties(i PROPERTIES COST 3.5)
add_test(j ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/j.py)
#set_tests_properties(j PROPERTIES COST 4.5)
Run the tests again and observe the order now.
Run the tests in parallel on several cores (if you have them available).
Discuss why it can be beneficial to define the COST if some tests take
much longer than others (we could have also reordered them manually).
Keypoints
Any custom command can be defined as a test in CMake.
Tests can be run through CTest.
CTest particularly shines when running sequential tests in parallel.
Built with Sphinx using a
theme
provided by Read the Docs.