I have a cmake build system but I am having trouble with the unit tests.
I would like them to run after a build has completed.
This is my CMakeLists.txt in the tests directory:
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Testing library
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
FetchContent_MakeAvailable(googletest)
# Adds googletest
add_library(googletest INTERFACE IMPORTED)
target_link_libraries(googletest INTERFACE gtest_main)
add_executable(mercury_tests basic_assertions.cpp vec3.test.cpp color.test.cpp)
target_link_libraries(mercury_tests
PRIVATE
googletest
michelangelo)
set(UNIT_TEST mercury_tests)
add_test(NAME ${UNIT_TEST} COMMAND ${UNIT_TEST})
add_custom_command(
TARGET ${UNIT_TEST}
COMMENT "Run tests"
POST_BUILD
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION> -R "^${UNIT_TEST}$" --output-on-failures
)
During building this is what I see:
[ 80%] Built target gtest
[ 84%] Built target gtest_main
Scanning dependencies of target mercury_tests
[ 86%] Building CXX object tests/CMakeFiles/mercury_tests.dir/vec3.test.cpp.o
[ 88%] Linking CXX executable mercury_tests
Run tests
Test project /workspaces/rtiow/build/tests
Start 1: mercury_tests
1/1 Test #1: mercury_tests .................... Passed 0.00 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.01 sec
[ 92%] Built target mercury_tests
[ 96%] Built target gmock
[100%] Built target gmock_main
All my unit tests run as a single test instead of the 25 or so tests I have.
If I force one of them to fail then it deletes the executable so it prevents me from running the tests after to see which has failed:
[ 80%] Built target gtest
[ 84%] Built target gtest_main
Scanning dependencies of target mercury_tests
[ 86%] Building CXX object tests/CMakeFiles/mercury_tests.dir/vec3.test.cpp.o
[ 88%] Linking CXX executable mercury_tests
Run tests
Test project /workspaces/rtiow/build/tests
Start 1: mercury_tests
1/1 Test #1: mercury_tests ....................***Failed 0.02 sec
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.02 sec
The following tests FAILED:
1 - mercury_tests (Failed)
Errors while running CTest
make[2]: *** [tests/CMakeFiles/mercury_tests.dir/build.make:138: tests/mercury_tests] Error 8
make[2]: *** Deleting file 'tests/mercury_tests'
make[1]: *** [CMakeFiles/Makefile2:1309: tests/CMakeFiles/mercury_tests.dir/all] Error 2
make: *** [Makefile:160: all] Error 2
How do I get the output I desire - the individuality of the tests - and it not to delete the unit test file if any of the tests fail?
Note: The mismatch between the test name (mercury) and the linked libraries (michelangelo) is because I had a fairly complete cmake file for a project called mercury and copied it into the michelangelo folder and I haven't got all the names switched over yet.