ctest complains "No test configuration file found!"
Asked Answered
P

2

8

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?

Predesignate answered 26/8, 2021 at 17:28 Comment(2)
You need to call ctest from build directory. Exactly this directory contains configuration file which ctest wants to read.Anarchy
@Anarchy As I feared it is only a minor matter... Please make your comment an answer. I will accept it.Predesignate
E
7

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.

Eyrir answered 12/10, 2023 at 8:44 Comment(2)
Thanks for clarification. I haven't noticed this because I enable_testing() in the root CMakeLists.txt that enables testing in my build/ directory. But anyway it's good to know. Btw. what do you mean with CMakeFile.txt? Isn't it CMakeLists.txt?Predesignate
Handy answer! When using VS2019 to build (after configuring with cmake), the CMake test file is located in build/VS2019, not in build.Baudin
A
10

The macro enable_testing() creates ctest configuration file in the build directory. For find this file, ctest needs to be run from that build directory.

Running ctest from the source directory has no sense, as it doesn't see results of CMake (unless you do in-source builds).

Anarchy answered 26/8, 2021 at 18:59 Comment(2)
"unless you do in-source builds"... which you absolutely should not.Microwatt
Starting with CMake 3.20, you can also run ctest --test-dir /path/to/tests according to another question: #38645241Ninetieth
E
7

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.

Eyrir answered 12/10, 2023 at 8:44 Comment(2)
Thanks for clarification. I haven't noticed this because I enable_testing() in the root CMakeLists.txt that enables testing in my build/ directory. But anyway it's good to know. Btw. what do you mean with CMakeFile.txt? Isn't it CMakeLists.txt?Predesignate
Handy answer! When using VS2019 to build (after configuring with cmake), the CMake test file is located in build/VS2019, not in build.Baudin

© 2022 - 2024 — McMap. All rights reserved.