Search code examples
cmakectest

Can't run ctest unless the test executable is named "tests"


I am trying to get CMake/CTest to work. My problem is that CTest does not seem to pick up a test executable unless it is named tests.

In my minimal example project I have a single CMakeLists.txt file:

cmake_minimum_required(VERSION 3.0)
project(myproject C)
enable_testing()
set(TEST_EXE_NAME tests)
add_executable(${TEST_EXE_NAME} test_main.c)
add_test(NAME "My tests" COMMAND ${TEST_EXE_NAME})

...and a very simple test program, test_main.c, that always passes:

int main() {
  return 0;
}

I can run make && make test, and all is fine as long as TEST_EXE_NAME is set to tests. However, when I change the executable name to something else, e.g. mytests, I get the following error:

Could not find executable tests
Looked in the following places:
tests
tests
Release/tests
Release/tests
Debug/tests
...

What am I missing?


Solution

  • According to the CMake manual for add_test() the test name may not contain spaces:

    The test name may not contain spaces, quotes, or other
    characters special in CMake syntax.
    

    In the problematic example, the test was named "My tests". Changing the test name to "My_tests" solves the problem.

    Apparently, the part of the test name after the space character (i.e. "tests") was interpreted as the test executable name.