运行 带有分开的 src- 和 test-folders 的 googletest

Running googletest with separated src- and test-folders

这是我的最小示例 (https://github.com/theCollectiv/cmake_minimal)。我首先通过单独的 Headerplacement (-> src 和 include)

获得了这个项目结构
.
├── 1Cmake.sh
├── 2runExe.sh
├── 3cleanTarget.sh
├── 4runTest.sh
├── 7VersionOfCurrentTools.sh
├── build
│   ├── CMakeCache.txt
│   ├──...
│   ...
├── CMakeDefaults.txt
├── CMakeLists.txt
├── compile_commands.json -> ...
├── data
├── docs
│   ├── ...
├── include
│   └── addition
│       └── addition.hpp
├── src
│   ├── addition
│   │   └── addition.cpp
│   ├── main.cpp
├── tests
│   └── test01_proofOfWork
│       ├── CMakeLists.txt
│       └── tests.cpp

main.cpp

#include "addition.hpp"

#include <iostream>
int main() {
  int a = 5;
  int b = 3;
  std::cout << "a is " << a << "\nb is " << b << "\nThe Sum of both "
<< add(a, b) << std::endl; }

addition.cpp

#include "addition.hpp"
int add(int a, int b) { return a + b; }

header 共 addition.cpp:addition.hpp

#pragma once
int add(int a, int b);

root-folder 中的 CMakeList.txt 看起来像这样:

cmake_minimum_required(VERSION 3.21.2)
set (This
    Project_main)

project(${This}
    LANGUAGES CXX
    VERSION 1.000)

enable_testing()
find_package(GTest REQUIRED)

add_subdirectory(./tests/test01_proofOfWork)

# Custom Variables
set(QUELLE src/main.cpp)
set(ZIEL finalExecutable)

# integrate lib (with .cpp and its header)
set(LIB_1 addition)

add_library(                                
    ${LIB_1}                                
    STATIC src/addition/addition.cpp        
    )
# add .hpp hinzufuegen and connect to lib
target_include_directories(              
    ${LIB_1}                             
    PUBLIC include/addition             
    )
# add executable
add_executable(${ZIEL} ${QUELLE})       

# linking the lib  
target_link_libraries(                  
    ${ZIEL}                             
    PRIVATE ${LIB_1}                    
    #    PUBLIC ${LIB_1}                    
    )

现在我想使用 googletest 添加单元测试。我正在使用 ubuntu 并且 googletest 安装在我的系统路径中(使用共享库)并且可以正常工作。

我现在有点卡壳了,如何将googletest很好地集成到项目中。

tests-dir中的CMakeList.txt:

# Name of the Tests
set (This
    runTest)

# Location of the test
set (Sources
    tests.cpp)

# executable of the test
add_executable(${This} ${Sources})

# linking libs for the test
target_link_libraries(${This} PUBLIC
    GTest::gmock
    GTest::gtest
    GTest::gmock_main
    GTest::gtest_main)
# registrating the test 
gtest_discover_tests(${This})

test.cpp

// tests.cpp
// #include "./../../include/addition/addition.hpp"
#include <gtest/gtest.h>
#include <gtest/internal/gtest-internal.h>

// bool foo(){
//     return true;
// }
// 
// 
// TEST(Simpletest, trueEqualsTrue) {
//     EXPECT_TRUE(foo());
//     EXPECT_FALSE(foo());
//     EXPECT_EQ(true, foo());
//     ASSERT_FALSE(foo());
//     EXPECT_EQ(false, foo());
// }
//
TEST(SquareRootTest, PositiveNos) {
    ASSERT_EQ(18.0, add(1,17));
    EXPECT_EQ(6, add(1,6));
    ASSERT_EQ(25.4, add(55, -1));
    ASSERT_EQ(0, add(-4, 4));
}


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

de-commented 测试代码工作和编译正常,所以 gtest 工作正常(意思是:如果我测试代码,那是在同一个文件中测试代码)。

但是...(我是新手,所以这可能是一个简单(或菜鸟)的问题): 我添加 googletest 的原因是为了测试 src- 或 -include 目录中的代码。因此,我一直在这些目录中为测试用例添加所有 headers 和文件。 有没有一种好方法可以让所有这些都可用于我可能 运行 而无需一直配置的可能测试?


我的问题是,

  1. de-commented 代码 works/compiles 很好(代码在与测试相同的位置)。这意味着:
    • googletest 安装正确
    • tests-CMakeLists.txt 工作正常(root-CmakeLists.txt 也正常工作)
  2. 如果我想编译 test-code,那不是 de-commented(这意味着 'code to test' 与测试本身不在同一个文件中),它正在抱怨缺少 headers。如果我添加它们,测试的编译会因错误而退出(比如“cmake line XXX”......没有告诉我任何东西或“未定义的参考”)。我遇到的问题
    • 它不编译测试(很明显)
    • 即使它可以编译,我也必须重建 test-directory 中的所有内容(在 test-source-code 中添加 header),就像我在 [=83= 中所做的那样] (similar/same 文件链接在他们两个)。如果我有一个更复杂的项目结构(在一个文件中使用一个函数,它使用另一个文件中的函数),这是 'doing the same exact same stuff in for the normal project (src and include) and in the tests-dir'。还是我现在错了?

解决方案:

# linking libs for the test
target_link_libraries(${This} PUBLIC
    GTest::gmock
    GTest::gtest
    GTest::gmock_main
    GTest::gtest_main
    # Solution: Got to link the lib(s) (which I want to test in the root-dir) against the test-executable
    addition
    )

您没有将您的测试项目链接到您的库。所以它不使用你的图书馆。 Link它。

target_link_libraries(${This} addition)