【问题标题】:No tests found while running gtest运行 gtest 时未找到测试
【发布时间】:2021-08-04 19:10:55
【问题描述】:

我正在使用 gtest 来测试我的 c++ 项目。我按照here的步骤操作

一切顺利,但是当我运行 cd build && ctest 命令时,我得到的输出为Test project C:/Users/admin/Desktop/last/build No tests were found!!!

我的 CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.14)
project(last)

# GoogleTest requires at least C++11
set(CMAKE_CXX_STANDARD 11)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(
  hello_test
  fact.cpp
  header.h
  main.cpp
  testt.cpp
)
target_link_libraries(
  hello_test
  gtest_main
)

include(GoogleTest)
gtest_discover_tests(hello_test)

【问题讨论】:

  • 你先运行 CMake 了吗?
  • 是的,我做了cmake -G"MSYS Makefiles" .. 然后cmake --build build
  • 我的 CMakeLists 文件有什么问题,我拥有的文件是 fact.cpp、main.cpp、testt.cpp、header.h

标签: c++ cmake googletest


【解决方案1】:

我在 CMakeLists 文件中更改了一行

add_executable(
  hello_test
  header.h
  main.cpp
  
)

并加了一行

add_test(testt.cpp hello_test)  

这给了我正确的输出并运行了测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2019-01-11
    • 2019-07-29
    • 1970-01-01
    • 2012-05-23
    • 2021-02-15
    相关资源
    最近更新 更多