【问题标题】:Running googletest with separated src- and test-folders使用分离的 src- 和 test- 文件夹运行 googletest
【发布时间】:2021-12-02 01:26:48
【问题描述】:

这是我的最小示例 (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; }

addition.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 很好地集成到项目中。

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

取消注释的测试代码可以正常工作和编译,所以 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
    )

【问题讨论】:

  • I'm now a little bit stuck 到底是什么? Is there a good way如何衡量“好”?您当前的设置是否有问题?
  • 感谢您的评论和反馈。我将我的问题(最后一段)扩展得更准确。如果还有更多不清楚的地方,请告诉我。 PS:此示例的代码github.com/theCollectiv/cmake_minimal
  • 您没有在 tests/CMakeLists.txt 中链接您的项目库
  • ike "cmake line XXX" ...doesnt tell me anything or "undefined reference too" 它不会告诉你——但它会告诉其他人。请发布确切的逐字详细错误消息,最多来自cmake --build &lt;builddir&gt; --verbosectest -VV。你不应该#include &lt;addition.hpp&gt;吗?
  • @Stephan:我是否总是必须再次在 tests-CMakeLists.txt 中再次链接项目库? ...如果项目变得更大,对于根 CMakeList.txt 中的每一个小变化,都很难保持模拟/根据项目结构。

标签: c++ cmake googletest


【解决方案1】:

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

target_link_libraries(${This} addition)

【讨论】:

  • 谢谢,现在它按预期工作-> 所以我可以测试“加法”。如果我在根项目中添加一个新库,我是否总是需要编辑这两个 CMakeLists.txt?没有办法绕过它,或者?
  • 我不明白。如果你添加一个新库并且你想在某个可执行文件中使用它,那么你必须与它链接。我相信这很简单。
猜你喜欢
  • 2020-08-25
  • 2016-03-27
  • 1970-01-01
  • 2013-05-22
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
相关资源
最近更新 更多