CMake 与 CTest 无法找到我用 gtest 编写的任何 google 测试

问题描述 投票:0回答:1

虽然我已经担任软件工程师十年了,但我对 cmake 和 Visual Studio 代码都是新手。我只是从来没有使用过这些工具。按照微软的文档,我将 vcpkg 设置为我的包管理器。

我已经像这样设置了我的 CMakeList.txt 。我有 2 个可执行文件。一个用于主项目,一个用于运行测试(RunTests.exe)

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)

但是 CTest 似乎没有接受任何测试。在使用 CMake 插件的 Visual Studio Code 1.89.1 的“测试”窗口中,我看到了我的项目名称,但其下没有测试。当我单击运行时,我得到以下输出。我用星号编辑了一些个人信息

[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]
cmake googletest vcpkg
1个回答
0
投票

我想通了。首先我发现 VS Code 有一个 CMake 调试器。非常便利!事实证明,在我的 RunTests 主文件中,我忽略了将 argc 和 argv 传递到testing::InitGoogleTest。因此,当 gtest_discover_tests 尝试在 RunTests.exe 上使用 --gtest-list-tests 时,它正在运行测试而不是列出它们。这不是预期的输出,它只是默默地失败了

© www.soinside.com 2019 - 2024. All rights reserved.