首页 > 解决方案 > 使用分离的 src- 和 test- 文件夹运行 googletest

问题描述

这是我的最小示例(https://github.com/theCollectiv/cmake_minimal)。我首先使用单独的 Headerplacement (-> src and 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

主文件

#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; }

加法.cpp

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

add.cpp 的头文件:addition.hpp

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

根文件夹中的 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 很好地集成到项目中。

测试目录中的 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})

测试.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();
}

取消注释的测试代码可以正常工作和编译,所以 gtest 可以正常工作(意味着:如果我正在测试代码,那就是在同一个文件中测试代码)。

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


我的问题是,

  1. 取消注释的代码可以正常工作/编译(在与测试相同的位置的代码)。这意味着:
    • googletest 安装正确
    • tests-CMakeLists.txt 工作正常(根 CmakeLists.txt 也是)
  2. 如果我想编译未取消注释的测试代码(这意味着“要测试的代码”与测试本身不在同一个文件中),它会抱怨缺少标头。如果我添加它们,则测试的编译会因错误而退出(例如“cmake line XXX”......不会告诉我任何事情或“未定义的引用”)。我遇到的问题
    • 它不编译测试(很明显)
    • 即使它可以编译,我也必须在测试目录中重建所有东西(在测试源代码中添加标头),就像我在根目录中所做的一样(它们都链接了相似/相同的文件)。如果我有一个更复杂的项目结构(在一个文件中使用一个函数,该函数使用另一个文件中的函数),这就是“在普通项目(src 和包含)和测试目录中做同样的事情”。还是我在这一点上错了?

解决方案:

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

标签: c++cmakegoogletest

解决方案


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

target_link_libraries(${This} addition)

推荐阅读