I want my tests to be launched each time my project is successfully built. And if some tests are broken I want my build to be broken too. By default I need to run tests manually by running ctest
command. CTest can actually build project but I use IDE that invokes make
to build sources. And make
doesn't run tests.
I add this command to my root CMakeLists.txt file but it doesn't work.
add_custom_command(OUTPUT tests.txt POST_BUILD COMMAND ctest --output-on-failure)
CMake doesn't return any errors and everything builds fine but my custom command doesn't invokes. How can I run something after each successful build in CMake?
Update:
My final solution is creating this macro:
macro(add_unit_test target target_test) set(UNIT_TEST_TARGETS ${UNIT_TEST_TARGETS} ${target_test} PARENT_SCOPE) add_test(target ${CMAKE_CURRENT_BINARY_DIR}/${target_test}) endmacro(add_unit_test)
It calls add_test
and remembers test target in a list. Every test in a project added by this macro. In the root CMakeLists.txt I have this code:
add_custom_target( all_tests ALL DEPENDS ${UNIT_TEST_TARGETS} ) add_custom_command(TARGET all_tests COMMENT "Run tests" POST_BUILD COMMAND ctest ARGS --output-on-failure WORKING_DIRECTORY ${CMAKE_BINARY_DIR} )
It creates custom target that depends on all unit tests in a project. Custom command is runs after all_tests
target was built.
CTest is an executable that comes with CMake; it handles running the tests for the project. While CTest works well with CMake, you do not have to use CMake in order to use CTest.
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.
Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.
Build and Test Mode. CTest provides a command-line signature to configure (i.e. run cmake on), build, and/or execute a test: ctest --build-and-test <path-to-source> <path-to-build> --build-generator <generator> [<options>...]
This form of add_custom_command
will only execute if another CMake target has a dependency on "tests.txt". I assume that no other target has "tests.txt" as an input file, hence the custom command never runs.
I think you could use the second form of add_custom_command
to achieve your goal; something like:
add_custom_command(TARGET MainTest POST_BUILD COMMAND ctest -C $<CONFIGURATION> --output-on-failure)
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