I have a CMake-based project that consists of several sub-components, which can all be independently compiled and tested. The directory layout looks like this:
.
├── CMakeLists.txt
├── comp1
│ ├── CMakeLists.txt
│ ├── src
│ │ ├── foo.cc
│ │ └── foo.h
│ └── tests
│ ├── CMakeLists.txt
│ └── test_comp1.cc
└── comp2
├── CMakeLists.txt
├── src
│ ├── bar.cc
│ └── bar.h
└── tests
├── CMakeLists.txt
└── test_comp2.cc
I want to enable ctest, therefore in the root CMakeLists.txt I have include(CTest)
and in the component-specific CMakeLists.txt files I have
if(BUILD_TESTING)
add_subdirectory(tests)
endif()
In compX/tests/CMakeLists.txt I have the code to compile the test and the add_test()
command. The tests get successfully compiled and I can manually run them. However, if I call ctest
, it returns
No tests were found!!!
After playing a bit with this, it turned out that if I move the add_subdirectory(tests)
call to the root CMakeLists.txt like this:
if(BUILD_TESTING)
add_subdirectory(comp1/tests)
endif()
it works. But I find this quite ugly and messy, to put component-specific stuff into the root file.
Conversely, I tried to move the include(CTest)
command one level down into the component-specific CMakeLists.txt. But ctest
complains with this:
*********************************
No test configuration file found!
*********************************
Is there seriously no way to use ctest with a directory structure like above?
comp1/CMakeLists.txt
disablesBUILD_TESTING
option, or replaceadd_test
command. Note, that if your callinclude(CTest)
in the subdirectory,ctest
should be executed in the subdirectory too; it won't work from the top-level directory. – Gunneryadd_test
"? – Keramicfunction(add_test)
. Anywhere, we could only guess what is wrong with yourtests/CMakeLists.txt
- you didn't show it in the question post. – Gunnerycmake --build build --target test
(UNIX) ?, otherwise ctest must be run from the binary directory whereenable_testing()
has been call (which is done internally by the macroinclude(CTest)
) src: gitlab.kitware.com/cmake/cmake/blob/master/Modules/… – Judoka