CTest not detecting tests
Asked Answered
C

1

36

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.

Cynthy answered 15/5, 2015 at 2:19 Comment(4)
Looks like a quote character is missing in the assignment statement set (MZL_INCLUDE_DIRS ... in the top CMakeLists.txt.Foredoom
@Foredoom It seems that this is a problem, but when I fix this and remove the unneeded include in MZL_INCLUDE_DIRS, the variable is no longer set in tests/CMakeLists.txt.Cynthy
It also seems that even if I fix that issue by just using .. as the include directory for the tests, there are still no tests found.Cynthy
Please update the listing of your CMakeLists.txt now.Foredoom
C
84

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)
Coffle answered 15/5, 2015 at 16:49 Comment(3)
Can't believe I missed this. This also fixed the issue with variables not being defined in child directories. Thanks!Cynthy
This is one of those examples where a simple note could make a developers live so much easier. No tests were found!!! Did you define enable_testing() before add_subdirectory()?Relax
@Relax +1 to your point. Furthermore wouldn't it be great if the GoogleTest quick start (google.github.io/googletest/quickstart-cmake.html) mentioned tests in subdirectories?Edee

© 2022 - 2024 — McMap. All rights reserved.