GoogleTest 在 C++ 项目中找不到单元测试

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

我一直在尝试为我的项目创建两个单独的可执行文件:一个用于项目本身,另一个用于运行单元测试。一切都编译正常,但是当我运行带有测试的可执行文件时,它说已发现 0 个测试:

[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[  PASSED  ] 0 tests.

我按照以下方式组织了该项目:

├── CMakeLists.txt
├── MyProject
│   ├── CMakeLists.txt
│   ├── common.hpp
│   ├── exceptions.hpp
│   ├── file_manager.cpp
│   ├── file_manager.hpp
│   ├── file_manager.test.cpp
│   ├── logging.cpp
│   ├── logging.hpp
│   ├── main.cpp
│   ├── speed_gauge.cpp
│   ├── speed_gauge.hpp
│   ├── speed_gauge.test.cpp
│   ├── test_runner.cpp
│   ├── tests_common.test.cpp
│   ├── tests_common.test.hpp
│   ├── word_randomizer.cpp
│   ├── word_randomizer.hpp
│   └── word_randomizer.test.cpp
├── build

这是我的

top-level CMakeLists.txt
文件:

cmake_minimum_required(VERSION 3.14)

project(MyProjectApp
        VERSION 1.0.0
        LANGUAGES CXX        
)


set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(buildTests OFF)

function(set_necessary_compile_options)

    foreach(target_file IN LISTS ARGN)
        target_compile_options(${target_file} PUBLIC -Wall -Wextra)
        target_compile_features(${target_file} PUBLIC cxx_std_17)

    endforeach()

endfunction()

add_subdirectory(MyProject)

add_executable(MyProjectExe "MyProject/main.cpp")
set_necessary_compile_options(MyProjectExe)
target_link_libraries(MyProjectExe MyProject_core)

if(buildTests)

    include(FetchContent)
    FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.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(MyProjectTestsExe "MyProject/test_runner.cpp")
    set_necessary_compile_options(MyProjectTestsExe)
    target_link_libraries(MyProjectTestsExe GTest::gtest_main MyProject_core MyProject_unit_tests)

    include(GoogleTest)
    gtest_discover_tests(MyProjectTestsExe)

endif()

这是

MyProject/CMakeLists.txt
文件:

add_library(MyProject_core file_manager.cpp word_randomizer.cpp speed_gauge.cpp logging.cpp)

if(buildTests)
       # Find Unit-Tests related files
        set(unit_test_files file_manager.test.cpp speed_gauge.test.cpp tests_common.test.cpp word_randomizer.test.cpp)

        add_library(MyProject_unit_tests ${unit_test_files}) 
endif()

test_runner.cpp
内容:

#include <gtest/gtest.h>

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

我尝试将每个单元测试文件编译为独立的可执行文件,但它不起作用,也没有发现任何测试。我的方法有什么问题,我该怎么做才能使 MyProjectTestsExe 运行所有测试? 附:所有测试文件都有 TEST 和 TEST_F 调用以及所有必要的包含。

c++ unit-testing cmake googletest
1个回答
0
投票

您似乎没有列出包含测试的源文件。 例如,你应该有这样的

add_executable(MyProjectTestsExe "MyProject/test_runner.cpp" "file_manager.test.cpp" ...)

现在,您的测试可执行文件仅包含 google main,但根本没有测试。因此,您应该将测试绑定/添加到该 exe。

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