【问题标题】:CTest not detecting testsCTest 未检测到测试
【发布时间】:2023-03-20 16:49:01
【问题描述】:

我有一个有结构的项目

├── 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

顶层目录CMakeLists.txt文件在哪里

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})

而tests/CMakeLists.txt 是

# 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})

在我添加第二个测试mzl-communication-test 之前一切正常。在添加它和# Communications test 之后的tests/CMakeLists.txt 中的行之后,运行ctest 并没有做任何额外的事情——它仍然只运行了第一个测试。

删除构建目录并再次运行cmake 后,初始CMake 运行没有错误,但运行make 再次运行CMake,导致CMake 出错:

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

随后出现 Make 错误:

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

再次运行make 会生成所有内容,包括测试;但是,运行 ctest 会导致

Test project build
No tests were found!!!

问题似乎与我正在使用 CMake 做的事情有关,但无法从这里弄清楚该怎么做,因为我看不出我正在做的任何事情与最初工作时不同。甚至我最后一次提交到 git 时,它在我提交时正在工作,现在也不再工作了。

【问题讨论】:

  • 在顶部CMakeLists.txt 的赋值语句set (MZL_INCLUDE_DIRS ... 中似乎缺少引号字符。
  • @sakra 看来这是个问题,但是当我解决这个问题并删除MZL_INCLUDE_DIRS 中不需要的include 时,tests/CMakeLists.txt 中不再设置变量。
  • 似乎即使我只使用..作为测试的包含目录来解决该问题,仍然没有找到测试。
  • 请立即更新您的 CMakeLists.txt 列表。

标签: cmake makefile ctest


【解决方案1】:

您需要先将enable_testing() 调用移动到add_subdirectory(tests) 之前

# Enable testing for the project
enable_testing()

add_subdirectory(tests)

【讨论】:

  • 不敢相信我错过了这个。这也解决了未在子目录中定义变量的问题。谢谢!
  • 这是一个简单的笔记可以让开发人员生活得更轻松的例子之一。 No tests were found!!! Did you define enable_testing() before add_subdirectory()?
  • @lanoxx +1 你的意思。此外,如果 GoogleTest 快速入门 (google.github.io/googletest/quickstart-cmake.html) 提到子目录中的测试不是很好吗?
猜你喜欢
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 2019-09-15
  • 2020-12-23
  • 1970-01-01
  • 2012-11-13
  • 2020-11-30
相关资源
最近更新 更多