7

我似乎没有让我的 CTest 项目识别我的 Catch2 测试。测试项目本身构建良好,我设法使用它创建的可执行文件运行测试。但是在运行时

ctest -V

我不断得到的输出是:

UpdateCTestConfiguration  from :/home/user/code/project/libs/DartConfiguration.tcl
UpdateCTestConfiguration  from :/home/user/code/project/libs/DartConfiguration.tcl
Test project /home/user/code/project/libs/
Constructing a list of tests
Updating test list for fixtures
Added 0 tests to meet fixture requirements
Checking test dependency graph...
Checking test dependency graph end
No tests were found!!!

我的设置如下:

文件夹结构:

libs
├── maths
│   ├── matrix.cpp / hpp
│   ├── function.cpp / hpp
│   └── CMakeLists.txt
├── ctest
│   ├── matrix_test.cpp
│   ├── function_test.cpp
│   └── CMakeLists.txt
├── build

   └── CMakeLists.txt

我从构建文件夹构建

cmake ..
cmake --build .
ctest -V

ctest 中的 CMakeLists.txt

set(SOURCE_FILES
    main.cpp
    maths/matrix_test.cpp
    maths/function_test.cpp
)

# find the Catch2 library 
find_package(Catch2 REQUIRED)

# create a test executable
add_executable(ctest ${SOURCE_FILES})

target_link_libraries(ctest maths Catch2::Catch2)

include(CTest)
include(ParseAndAddCatchTests)
ParseAndAddCatchTests(ctest)

主 CMakeLists.txt

# set CMake version
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)

project(mathlib LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_SHARED_LIBRARY_PREFIX "lib")

include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})

set(LIB_BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../libs")

find_package(Catch2 REQUIRED)
enable_testing()

add_subdirectory("${LIB_BASE_DIR}/maths" maths)
add_subdirectory("${LIB_BASE_DIR}/ctest" ctest)

include(CTest)
include(Catch)
catch_discover_tests(ctest)

add_test(
    NAME catch_test
    COMMAND $<TARGET-FILE>:ctest --success
)

我已经查看了这里的食谱、cmake 食谱、modern-cmake、catch2 github 页面,但我似乎错过了一些明显的东西,因为由于某种原因,测试没有被选中。我无法找到带有 add_subdirectory 的示例,因此可能会导致不同的设置。

如果我跑

./bin/ctest

所有测试运行良好。所以可执行文件本身很好。

4

0 回答 0