With CMake I try to execute a simple test using CTest. I have this configuration all in one directory (${CMAKE_SOURCE_DIR}):
~$ cat hello.cpp
#include <iostream>
int main() {
std::cout << "hello world\n";
return(0);
}
~$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(TEST_CTEST)
enable_testing()
add_executable(test_ctest hello.cpp)
add_test(my_test ./build/test_ctest)
Then I execute:
~$ rm -rf Testing/ build/
~$ cmake -S . -B build
~$ cmake --build build
~$ ./build/test_ctest
hello world
~$ ctest
*********************************
No test configuration file found!
*********************************
Usage
ctest [options]
The only hint I have found in other Q&A is to place enable_testing()
into the root CMakeLists.txt
. I feel like I just missed a little thing, but I can't find what.
How do I run a test with CTest?
The answer from @Tsyvarev is not entierly true. You have to execute ctest
not only from the build directory, but also from the relative folder inside your build-directory, where enable_testing()
was called relatively to the CMAKE_SOURCE_DIR
directory.
So, e.g. when the CMakeLists.txt
, which calls enable_testing()
is located at proj/tests
and your build directory is proj/build
. Then you have to run ctest
in proj/build/tests
.