I don't seem to get my CTest project to recognize my Catch2 tests. The test project itself builds fine and I manage to run the tests using the executable created by it. However when running
ctest -V
The output I keep getting is:
UpdateCTestConfiguration from :/home/user/code/project/libs/DartConfiguration.tcl
UpdateCTestConfiguration from :/home/user/code/project/libs/DartConfiguration.tcl
Test project /home/user/code/project/libs/
Constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
No tests were found!!!
My set-up is as follows:
The folder structure:
libs
├── maths
│ ├── matrix.cpp / hpp
│ ├── function.cpp / hpp
│ └── CMakeLists.txt
├── ctest
│ ├── matrix_test.cpp
│ ├── function_test.cpp
│ └── CMakeLists.txt
├── build
└── CMakeLists.txt
I build from the build folder
cmake ..
cmake --build .
ctest -V
CMakeLists.txt in ctest
set(SOURCE_FILES
main.cpp
maths/matrix_test.cpp
maths/function_test.cpp
)
# find the Catch2 library
find_package(Catch2 REQUIRED)
# create a test executable
add_executable(ctest ${SOURCE_FILES})
target_link_libraries(ctest maths Catch2::Catch2)
include(CTest)
include(ParseAndAddCatchTests)
ParseAndAddCatchTests(ctest)
The main CMakeLists.txt
# set CMake version
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(mathlib LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
set(LIB_BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../libs")
find_package(Catch2 REQUIRED)
enable_testing()
add_subdirectory("${LIB_BASE_DIR}/maths" maths)
add_subdirectory("${LIB_BASE_DIR}/ctest" ctest)
include(CTest)
include(Catch)
catch_discover_tests(ctest)
add_test(
NAME catch_test
COMMAND $<TARGET-FILE>:ctest --success
)
I have looked at the recipes here, the cmake cookbook, modern-cmake, the catch2 github page, but I seem to miss something obvious as for some reason the tests do not get picked up. I have not been able to find an example with add_subdirectory so may be that leads to a different set-up.
If I run
./bin/ctest
all tests run fine. So the executable itself is fine.