运行ctest时,我收到以下错误消息:
Test project /home/path_to_my_project/build
No tests were found!!!
实际上还不清楚这个错误到底意味着什么。
根据官方文档,我调用根文件夹中的include(CTest)
,然后调用:
if(BUILD_TESTING)
add_subdirectory(test)
endif()
测试文件编译得很好,最终会放在my_project/build/subprojects/Build/my_project_core/test/testBin
文件夹中。
因此,我的猜测是ctest没有在它期望的位置找到文件,但错误消息没有显示任何有用的信息来修复这个错误。
我认为,有一些关于ctest如何工作以及它所期望的内容的一般上下文将有助于用户面对这个错误。
代码
根:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(foo)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set_property(DIRECTORY PROPERTY EP_BASE ${CMAKE_BINARY_DIR}/subprojects)
set(STAGED_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/stage)
include(ExternalProject)
ExternalProject_Add(
${PROJECT_NAME}_core
SOURCE_DIR
${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}_core
CMAKE_ARGS
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
-DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
-DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}
CMAKE_CACHE_ARGS
-DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
BUILD_ALWAYS
1
INSTALL_COMMAND
""
)
外部项目:
# external/upstream/CMakeLists.txt
add_subdirectory(boost)
外部伪助推(如果找不到系统版本,则不执行任何操作(:
# external/upstream/boost/CMakeLists.txt
find_package(Boost ${Boost_MINIMUM_REQUIRED} QUIET CONFIG COMPONENTS "${BOOST_COMPONENTS_REQUIRED}")
if(Boost_FOUND)
message(STATUS "Found Boost: ${_loc} (version ${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION}.${Boost_SUBMINOR_VERSION})")
add_library(boost_external INTERFACE)
else()
message(STATUS "Boost ${Boost_MINIMUM_REQUIRED} could not be located, downloading and building Boost 1.72.0 instead.")
endif()
核心项目:
# foo_core/CMakeLists.txt
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project("foo" LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(${PROJECT_NAME} INTERFACE)
# add alias so the project can be used with add_subdirectory
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
enable_testing()
add_subdirectory(test)
测试:
# foo_core/test/CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
# Include Boost as an imported target
find_package(Boost REQUIRED CONFIG COMPONENTS unit_test_framework)
# Keep test files in a separate source directory called test
file(GLOB TEST_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} unit_test/*.cpp)
#Run through each source
foreach(testSrc ${TEST_SRCS})
#Extract the filename without an extension (NAME_WE)
get_filename_component(testName ${testSrc} NAME_WE)
#Add compile target
add_executable(${testName} ${testSrc})
#link to Boost libraries AND your targets and dependencies
target_link_libraries(${testName} Boost::unit_test_framework )
#I like to move testing binaries into a testBin directory
set_target_properties(${testName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/testBin)
target_include_directories(
${testName} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
#Finally add it to test execution - Notice the WORKING_DIRECTORY and COMMAND
add_test(NAME ${testName}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/testBin
COMMAND ${CMAKE_BINARY_DIR}/test/testBin/${testName} )
endforeach(testSrc)
foo_core/include/foo/bar.h
#ifndef __BAR_H_INCLUDED__
#define __BAR_H_INCLUDED__
template<class T>
T bar(T a, T b){ return a*b;};
#endif
foo_core/test/unit_test/foo_test.cpp
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE foo_test
#include <boost/test/unit_test.hpp>
namespace utf = boost::unit_test;
#include <foo/bar.h>
BOOST_AUTO_TEST_SUITE( bar_suite )
BOOST_AUTO_TEST_CASE( bar_case )
{
BOOST_CHECK_EQUAL(bar(2,2), 4);
}
BOOST_AUTO_TEST_SUITE_END()
添加
ENABLE_TESTING()
在子项目中的AddTest之前。您不仅需要在head Cmake项目配置中添加此命令,还需要在子项目配置文件中添加该命令。