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

bkkx9g8r  于 6个月前  发布在  Go
关注(0)|答案(1)|浏览(86)

我一直在尝试为我的项目创建两个独立的可执行文件:一个用于项目本身,另一个用于运行单元测试。一切都编译正常,但当我运行可执行文件和测试时,它说发现了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运行所有测试?P.S.所有测试文件都有TEST和TEST_F的调用以及所有必要的包含。

6xfqseft

6xfqseft1#

看起来你没有列出包含测试的源文件。

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

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

相关问题