I am trying to make the passing of tests part of the build process.
Here I use add_custom_command
to run the test as a POST_BUILD
step.
function(register_test NAME)
add_test(${NAME} ${NAME})
# make the test run as part of the build process
add_custom_command(TARGET ${NAME} POST_BUILD COMMAND ${NAME})
endfunction()
The problem with this approach is that the test is only run when the target is built:
$ make
[ 50%] Built target lib1 Linking CXX executable ../../Debug/bin/lib1_test Running 1 test case... main.cpp(8): fatal error: in "lib1_test": critical check lib1() == "lib1" has failed [error != lib1] *** 1 failure is detected in the test module "Master Test Suite" make[2]: *** [lib1/test/lib1_test] Error 201 make[1]: *** [lib1/test/CMakeFiles/lib1_test.dir/all] Error 2 make: *** [all] Error 2
If the target doesn't need to be built, then the test is not run, and the build passes.
Here I don't make any changes, just rerun the build process
$ make
[ 50%] Built target lib1 [100%] Built target lib1_test
However, if lib1_test
is actually run, the test fails.
$ ./lib1/test/lib1_test
Running 1 test case... main.cpp(8): fatal error: in "lib1_test": critical check lib1() == "lib1" has failed [error != lib1] *** 1 failure is detected in the test module "Master Test Suite"
A better way to do this would be to make a lib1_test.passed
target which depends on lib1_test
, runs the tests, and is only created if the tests pass.
What I have tried:
I have tried using add_custom_target
to create a target lib1_test.passed
which depends on lib1_test
, and if successful, creates a file lib1_test.passed
:
add_custom_target(${NAME}.passed
DEPENDS ${NAME}
COMMAND ${NAME}
COMMAND ${CMAKE_COMMAND} -E touch ${NAME}.passed)
There are 2 shortfalls with what I have currently achieved:
- The running of the test is not part of the normal build process.
That is,make
will not "build"lib1_test.passed
;
I have to explicitly statemake lib1_test.passed
make lib1_test.passed
will always executelib1_test
, regardless of whetherlib1_test.passed
is newer thanlib1_test1
or not
Question:
How can I make the running of tests part of the build, where a failing test will be always rerun?
As shown in @user3159253 answer you need an output file (with a time stamp) for the build environment to check if it has to "build" the target in question again. So if your executable was successfully build - even if the subsequent call to run it fails - won't build again.
I wanted to add a solution where you can have only one target. It will rename the test executable output, run it and - if successful - rename it again to its original name to sort of mark it as "passed":
Here what I've got so far. The implementation is pretty quick and dirty but nevertheless it works. Please check and tell if it satisfies your needs.
CMakeLists.txt:
Source files for the sake of completeness:
lib.c:
lib.h:
test.c:
Unfortunately it's impossible to depend directly on
test
target due to CMake bug so we have to perform sunset manually.