You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CMake+CTest无法识别Google Test测试用例的问题求助

问题:CTest无法识别Google Test测试用例

我有十年软件开发经验,但刚接触CMake和Visual Studio Code,已按微软文档配置vcpkg作为包管理器。编写CMakeLists.txt生成主程序Geodesic-RayTracer与测试程序Runtests,已链接Google Test相关库并调用gtest_discover_tests,但CTest无法识别任何测试用例:

  • VS Code 1.89.1的CMake插件测试窗口仅显示项目名称,无测试项
  • 运行测试时CTest提示No tests were found!!!

相关配置与日志

CMakeLists.txt代码

cmake_minimum_required(VERSION 3.10)

project(Geodesic-RayTracer)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_executable(Geodesic-RayTracer Source/Main.cpp)

# gtest stuff TODO make this a add_subdirectory with its own cmakelist
find_package(gtest CONFIG REQUIRED)
include(CTest)
include(GoogleTest)
add_executable(Runtests Source/Tests/Runtests.cpp)
target_link_libraries(Runtests PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # For Windows: Prevent overriding the parent project's compiler/linker settings

#TODO see if I can make this automatically pick up .cpp files
target_sources(Runtests PRIVATE Source/Tests/ExampleTest.cpp)

gtest_discover_tests(Runtests)

运行日志

[main] Building folder: Geodesic-RayCaster 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build C:/Users/***/Desktop/Geodesic-RayCaster/build --parallel 18
[build] ninja: no work to do.
[driver] Build completed: 00:00:00.062
[build] Build finished with exit code 0
[proc] Executing command: "C:\Program Files\CMake\bin\ctest.exe" -T test --output-on-failure -R ^c\:/users/***/desktop/geodesic-raycaster$
[cpptools] The build configurations generated do not contain the active build configuration. Using "Debug" for CMAKE_BUILD_TYPE instead of "null" to ensure that IntelliSense configurations can be found
[ctest]    Site: ****
[ctest]    Build name: Win32-ninja
[ctest] Test project C:/Users/****/Desktop/Geodesic-RayCaster/build
[ctest] No tests were found!!!
[ctest] CTest finished with return code 0
[ctest] [object Object]

排查与解决步骤

1. 升级CMake最低版本

gtest_discover_tests在CMake 3.10中功能有限,建议将最低版本提升至3.16+,优化测试发现逻辑:

cmake_minimum_required(VERSION 3.16)

2. 调整gtest_discover_tests参数

确保在测试目标配置完成后调用,显式指定工作目录避免路径解析问题:

gtest_discover_tests(Runtests
  WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

3. 验证测试程序合法性

  • 确认Runtests.cpp未自定义main函数,避免覆盖GTest::gtest_main提供的入口
  • 检查ExampleTest.cpp的测试用例语法是否正确,示例:
#include <gtest/gtest.h>

TEST(ExampleSuite, BasicTest) {
  EXPECT_EQ(1+1, 2);
}

4. 修复CTest过滤规则错误

日志中CTest的-R ^c\:/users/***/desktop/geodesic-raycaster$是无效的测试名称过滤规则:

  • 打开VS Code设置,搜索cmake.testRegex,设置为空或正确规则(如^Runtests$)
  • 手动在终端运行CTest跳过错误过滤:
cd build
ctest -C Debug --output-on-failure

5. 明确构建类型

添加构建类型默认配置,避免CMake自动 fallback 导致的问题:

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
endif()

6. 手动验证测试二进制

检查build/bin/Runtests.exe是否存在,手动运行看是否输出测试结果:

./build/bin/Runtests.exe

如果手动运行能看到测试,说明是CTest发现逻辑问题;如果无输出,需排查测试程序的链接或测试用例定义。


内容的提问来源于stack exchange,提问作者Adam Sturge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 05:28:10