CTest misparsing add_test calls - cmake

I am trying to run some tests using CTest.
My CTestTestFile.cmake contains a single test
add_test(NAME test1
COMMAND python script.py args
)
but when I try run ctest it outputs the following
Start 1: NAME
Could not find executable test1
Looked in the following places:
test1
test1.exe
Release/test1
Release/test1.exe
Debug/test1
Debug/test1.exe
MinSizeRel/test1
MinSizeRel/test1.exe
RelWithDebInfo/test1
RelWithDebInfo/test1.exe
Deployment/test1
Deployment/test1.exe
Development/test1
Development/test1.exe
Unable to find executable: test1
1/1 Test #1: NAME .............................***Not Run 0.00 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.14 sec
The following tests FAILED:
1 - NAME (Not Run)
This suggests CTest thinks I'm using the short version of the command, so that my test's name is NAME, the command is test1 and python script.py args are the arguments to the command, when this is of course not the case, as I'm obviously using the long version of the command.
I have CMake version 3.24.1 installed.

The CTest runtime, unlike CMake, accepts only the "basic" signature documented at the end of the usual add_test documentation:
add_test(<name> <command> [<arg>...])
Notice that CTest thought your test was named NAME.
So you have to write:
add_test(test1 "python" "script.py" "args")

Related

ctest run only existing tests

I have some tests in my build that are optional. My CMakeLists.txt looks approximately like:
add_custom_target(all-tests)
add_executable(A ...)
add_dependencies(all-tests A)
add_test(NAME A COMMAND ...)
add_executable(B ...)
add_dependencies(all-tests B)
add_test(NAME B COMMAND ...)
## optional
add_executable(C EXCLUDE_FROM_ALL ...)
add_test(NAME C COMMAND ...)
The idea being that I can run
$ make all-tests
$ ctest
To both compile all my unit tests and then run them. The problem is, since C is optional, it doesn't build (all desired). And since it didn't build, it doesn't exist, and gets reported as having failed:
The following tests FAILED:
6 - C (Not Run)
Errors while running CTest
Is there a way to ignore this failure / have CTest not try to run tests that don't exist / otherwise express the idea of an optional test? I have a class of these optional tests that have similar names, so it'd be nice to be able to use ctest -R to run them, if they are built, rather than having to configure some script or other approach.
Make the optional tests depend on the existence of the built test executables by setting the REQUIRED_FILES property:
add_test(NAME C COMMAND ...)
add_executable(C EXCLUDE_FROM_ALL ...)
set_property(TEST C PROPERTY REQUIRED_FILES "$<TARGET_FILE:C>")
The use of the generator expression TARGET_FILE requires CMake 3.0 or later.

CTest: How to skip other tests if the "precheck" test fails?

I'm adding tests to my project and all the tests have some prerequisites so I add a precheck test which all other tests depend on. If the precheck fails then I'd like the other tests to stop immediately.
add_test(
NAME precheck
COMMAND false
)
add_test(
NAME test-1
COMMAND true
)
add_test(
NAME test-2
COMMAND true
)
set_tests_properties(
test-1 test-2
PROPERTIES
DEPENDS precheck
)
But seems like the DEPENDS property only impact the order of tests:
$ make test
Running tests...
Test project /root/ibvq/frkcrpg/b
Start 1: precheck
1/3 Test #1: precheck .........................***Failed 0.00 sec
Start 2: test-1
2/3 Test #2: test-1 ........................... Passed 0.00 sec
Start 3: test-2
3/3 Test #3: test-2 ........................... Passed 0.00 sec
67% tests passed, 1 tests failed out of 3
Total Test time (real) = 0.02 sec
The following tests FAILED:
1 - precheck (Failed)
Errors while running CTest
Makefile:83: recipe for target 'test' failed
make: *** [test] Error 8
So how can I make the failed precheck stop other tests?
If you are using CMake version 3.7 or later, you can use the test fixture related properties.
For earlier versions of CMake, have your precheck test create a dummy file on success that your other tests depend on by setting the REQUIRED_FILES property.

How to force cmake to write test output after make test

I'm building fortran project with cmake and I can't find solution to print to console FRUIT test results, they look something like these:
Test module initialized
. : successful assert, F : failed assert
7.00000000000000 -3.60000000000000 7.00000000000000
FFF
Start of FRUIT summary:
Some tests failed!
-- Failed assertion messages:
[_not_set_]:Expected [7.00000000000000], Got [1.00000000000000]
[_not_set_]:Expected [-3.60000000000000], Got [2.00000000000000]
[_not_set_]:Expected [7.00000000000000], Got [6.00000000000000]
-- end of failed assertion messages.
Total asserts : 3
Successful : 0
Failed : 3
Successful rate: 0.00%
Successful asserts / total asserts : [ 0 / 3 ]
Successful cases / total cases : [ 0 / 0 ]
-- end of FRUIT summary
The output I'm getting with make test looks like:
make test
Running tests...
Test project /home/konrad/Desktop/fortran
Start 1: unit_tests
1/1 Test #1: unit_tests ....................... Passed 0.01 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.01 sec
And since passing cmake tests doesn't mean passing FRUIT ones, I want to print FRUIT file everytime I run tests (just for the sake of making it work). I've tried adding printing commands at the end of test command (like this less), adding
-P ${CMAKE_TEST_DIR}/unit_tests.txt
at the end of add_test, building custom after build commands (which I can't make to run after make test so if you knew how to do that would solve it as well, seems like make test or test is not really a target)
Last part of my cmake file with all the testing code:
add_executable(task ${TASK_SOURCES})
add_executable(tests ${TEST_SOURCES})
enable_testing()
set(run_command "${CMAKE_BINARY_DIR}/tests")
set(UNIT_TEST_NAME "unit_tests.txt")
file(MAKE_DIRECTORY ${CMAKE_TEST_DIR})
add_test( NAME unit_tests
COMMAND sh -c
"rm -f ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME} \
&& ${run_command} \
>> ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME} \
&& less ${CMAKE_TEST_DIR}/${UNIT_TEST_NAME}"
)
I have solved a lack of tests output with custom CMake target that will invoke ctest in verbose mode etc.
e.g.
enable_testing()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
--force-new-ctest-process
--verbose
--output-on-failure
)
The output of cmake tests is captured to a file (Testing/Temporary/LastTest.log in my current project).
cmake tests rely on the return code of the test program, with one test program per test.
If you wish to run a program that is a "driver" for your tests, I recommend to use add_custom_target. This commands will add a target that runs a command of your choice.
For instance:
add_custom_target(Name unit_tests tests)
add_dependencies(unit_tests tests)
I am not sure whether the add_dependencies line is needed in this case though (as tests is a target managed by cmake).
Then, you can run
make unit_tests
and it will run your test driver.

CMake py.test detection?

py.test is know tool for testing Python scripts. I am missing, however, CMake macro for the detetion of py.test.
Is there anything of this kind within CMake pool of macros ?
Directly calling py.test from a CMakeLists.txt isn't very portable, because different systems name it quite differently, e.g.:
py.test # pip, as of early 2018
pytest # ditto
py.test-3 # Fedora 26 package
pytest-3 # ditto
Of course, the system might default to version 2 or 3, and might have both version 2 and 3 installed side by side.
Thus, a simple method for reliable executing right pytest version via cmake is to invoke pytest differently:
python3 -m pytest
(instead of py.test or pytest or ...)
If you want to test in cmake if the pytest package is available you can test this with execute_process(), e.g.:
execute_process(COMMAND python3 -m pytest --version
OUTPUT_VARIABLE PYTEST_output
ERROR_VARIABLE PYTEST_error
RESULT_VARIABLE PYTEST_result)
if(${PYTEST_result} UNEQUAL 0)
message(SEND_ERROR "Pytest package not available: ${PYTEST_error}")
endif()

Calling Unittest++ from cmake created makefile

I recently started learning cmake, and have run into a small issue. I got both my executable and the unit tests to compile from the generated makefile without issue. If I run ./test in the build directory, the tests created in UnitTest++ run and complete as expected, printing the results. Is there any way to get make test to simply run the test executable rather than running it inside ctest framework or should I go about this a different way?
Here is a minimal working example of my code:
src/main/main.c is a simple empty main function
src/test/testMain.cpp:
#include <UnitTest++/UnitTest++.h>
TEST(FailSpect)
{
CHECK(false);
}
int main()
{
UnitTest::RunAllTests();
}
CMakeLists.txt:
cmake_minimum_required( VERSION 2.6 )
project( myProject)
enable_testing()
set( myProjectMain
src/main/main.c
)
set( myProjectSrc
)
set( myProjectTestSrc
src/test/testMain.cpp
)
add_executable( myExecutable ${myProjectMain} ${myProjectSrc} )
add_executable( testSuite ${myProjectTestSrc} ${myProjectSrc} )
target_link_libraries( testSuite UnitTest++ )
add_test( testExe testSuite )
make test output:
Running tests...
Start processing tests
Test project /myProjectDir/build
1/ 1 Testing testExe Passed
100% tests passed, 0 tests failed out of 1
./testSuite output:
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
I have sorted out how to do this. First remove the lines:
enable_testing()
and
add_test(testExe testSuite)
and replace them by the line:
add_custom_target(test ./testExe
DEPENDS ./testExe)
at the end of the CMakeLists.txt file. Now make (all) builds both the tests and the main program. If everything is built already, then make test will just check that the tests are built and run them, producing:
[100%] Built target testExe
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
[100%] Built target test
If the tests are out of date (after a make clean for instance), then make test will produce:
[100%] Building CXX object CMakeFiles/testExe.dir/src/test/testMain.cpp.o
Linking CXX executable testExe
[100%] Built target testExe
/myProjectDir/src/test/testMain.cpp:5: error: Failure in FailSpect: false
FAILURE: 1 out of 1 tests failed (1 failures).
Test time: 0.00 seconds.
[100%] Built target test