CTest not detecting tests

Daniel Underwood picture Daniel Underwood · May 15, 2015 · Viewed 11.5k times · Source

I have a project with a structure

├── CMakeLists.txt
├── mzl.c
├── mzl.h
└── tests
    ├── CMakeLists.txt
    ├── mzl-communication-test.c
    ├── mzl-setup-test.c
    ├── mzl-test-errors.c
    └── mzl-test-errors.h

Where the top directory CMakeLists.txt file is

project(mzl)
cmake_minimum_required(VERSION 2.8)

add_subdirectory(tests)

# Enable testing for the project
enable_testing()

# Find zmq
find_library(ZMQ_LIB zmq REQUIRED)
message(STATUS "ZMQ Library: ${ZMQ_LIB}")

# Find threading library
set(CMAKE_THREAD_PREFER_PTHREAD ON)
find_package(Threads REQUIRED)
message(STATUS "Threading Library: ${CMAKE_THREAD_LIBS_INIT}")



# Set include directories for headers
set     (
    MZL_INCLUDE_DIRS
    ${CMAKE_SOURCE_DIR}
    CACHE STRING "MZL Include Directories"
    )
include_directories(${MZL_INCLUDE_DIRS})

# Set source files
set     (
    MZL_SRC_FILES
    mzl.c
    )

# Add library
add_library(${PROJECT_NAME} STATIC ${MZL_SRC_FILES})

# Link to zmq
target_link_libraries(${PROJECT_NAME} ${ZMQ_LIB} ${CMAKE_THREAD_LIBS_INIT})

and tests/CMakeLists.txt is

# Use MZL Source Directories (mzl headers are in top directory)
include_directories(${CMAKE_SOURCE_DIR})

# Variable from here is empty
message(STATUS "MZL Include Directories: ${MZL_INCLUDE_DIRS}")

# Files common to all tests
set ( TEST_COMMON_SOURCES
    mzl-test-errors.c
    )

# Library setup/shutdown testing
set(SETUP_TEST_NAME mzl-setup-test)
add_executable(${SETUP_TEST_NAME} ${TEST_COMMON_SOURCES} ${SETUP_TEST_NAME}.c)
target_link_libraries(${SETUP_TEST_NAME} ${PROJECT_NAME} ${ZMQ_LIB})
add_test(${SETUP_TEST_NAME} ${SETUP_TEST_NAME})

# Communcations test
set(COMMUNICATION_TEST_NAME mzl-communication-test)
add_executable(${COMMUNICATION_TEST_NAME} ${TEST_COMMON_SOURCES}
    ${COMMUNICATION_TEST_NAME}.c)
target_link_libraries(${COMMUNICATION_TEST_NAME} ${PROJECT_NAME}
        ${CMAKE_THREAD_LIBS_INIT} ${ZMQ_LIB})
add_test(${COMMUNICATION_TEST_NAME}  ${COMMUNICATION_TEST_NAME})

Everything worked fine before I added the second test mzl-communication-test. After adding it and the lines in the tests/CMakeLists.txt after # Communications test, running ctest did nothing extra -- it still only ran the first test.

After deleting the build directory and running cmake again, I get no errors for the initial CMake run, but running make runs CMake again, resulting in an error with CMake:

CMake Error: Parse error in cache file build/CMakeCache.txt. Offending entry: include

followed by a Make error:

Makefile:203: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1

Running make again results in everything being built, including the tests; however, running ctest results in

Test project build
No tests were found!!!

The issue seems to be with something that I am doing with CMake, but can't figure out what to do from here as I can't see anything that I'm doing differently than when it was originally working. Even my last commit to git, which was working when I committed it, is no longer working.

Answer

mshildt picture mshildt · May 15, 2015

You need to move the enable_testing() call to be before you do add_subdirectory(tests)

# Enable testing for the project
enable_testing()

add_subdirectory(tests)