Search code examples
c++unit-testingcmakegoogletest

GoogleTest not finding unit-tests in a C++ project


I've been trying to create two separate executables for my project: one for the project itself, and the other is for running unit-tests. Everything compiles okay, but when I run the executable with the tests, it says that 0 tests have been discovered:

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

I have organized the project in the following way:

├── 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

Here is my top-level CMakeLists.txt file:

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()

And here is MyProject/CMakeLists.txt file:

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()

And test_runner.cpp contents:

#include <gtest/gtest.h>

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

I've tried to compile each unit-test file as a standalone executable, but it didn't work and no tests have been discovered either. What is wrong with my approach and what can I do to make MyProjectTestsExe run all tests? P.S. All test files do have TEST's and TEST_F's calls and all necessary includes.


Solution

  • It seems that you did not list your source files containing tests. For example, you should have it like

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

    Right now, your test executable only contains google main, but there is no tests at all. So, you should tie/add tests to that exe.