【问题标题】:How do I make my cmake targets for catch2 testing and project running more extendable and sensible?如何使我的 cmake 目标用于 catch2 测试和项目运行更可扩展和更合理?
【发布时间】:2019-09-21 14:31:34
【问题描述】:

经过大量的努力和研究,我设法制作了多个 cmake 目标,以将运行我的程序与运行代码测试分开。但我不喜欢我所做的,因为我在 CMakeList.txt 文件中看到了冗余。

目前我必须将每个新的源文件添加到两个目标中,以便源目标可以使用该文件构建,并且测试目标可以构建,因为他们需要测试该文件。我不能将整个源目标放入测试目标,因为测试目标将包含两个主文件。

我对如何修复冗余的唯一想法是将源目标中没有main.cpp 文件的所有文件放入某个组,然后将该组附加到两个目标。这样,源目标仅包含main.cpp 文件和源文件组,而测试目标包含其所有测试和源文件组。所以文件组基本上是两个目标之间的所有重叠。我只是不知道该怎么做。

研究

这是我发现的其他堆栈溢出问题,它们帮助我到达了现在的位置:

代码

这是我用来试验 catch2 和 cmake 的测试项目,目前适用于构建目标“tests”和“catch2Test”:

/catch2Test      // <-- project folder
  |---- /include
  |       |---- /catch2
  |               |---- catch.hpp
  |---- /src
  |       |---- /myMath
  |       |       |---- factorial.cpp
  |       |       |---- factorial.h
  |       |---- main.cpp
  |       |---- CMakeLists.txt
  |---- /test
  |       |---- test_main.cpp
  |       |---- test_factorial.cpp
  |       |---- CMakeLists.txt
  |---- CMakeLists.txt

/include/catch2/catch.hpp是catch2的库头文件

/src/myMath/ 包含用于阶乘实现的头文件和代码文件,与catch2 tutorial 中使用的相同。这也是阶乘测试的实现来源。

/src/main.cpp 是一个简单的主文件,其中包含 factorial.h 以进行阶乘运算,然后将其打印到 cout。

以下是实际重要的其余文件的显式代码:

/test/test_main.cpp

#define CATCH_CONFIG_MAIN
#include "../include/catch2/catch.hpp"

/test/test_factorial.cpp

#include "../include/catch2/catch.hpp"
#include "../src/myMath/factorial.h"

TEST_CASE("Factorials are computed", "[factorial]")
{
    REQUIRE(factorial(0) == 1);
    REQUIRE(factorial(1) == 1);
    REQUIRE(factorial(2) == 2);
    REQUIRE(factorial(3) == 6);
    REQUIRE(factorial(10) == 3628800);
}

/CmakeLists.txt

cmake_minimum_required(VERSION 3.13)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(src)
add_subdirectory(test)

/test/CMakeLists.txt

add_executable(tests
        ../src/myMath/factorial.cpp ../src/myMath/factorial.h

        ../include/catch2/catch.hpp

        test_main.cpp
        test_factorial.cpp
)

/src/CMakeLists.txt

add_executable(catch2Test
        main.cpp
        myMath/factorial.cpp myMath/factorial.h
)

跑步

当我运行 catch2Test 目标时,我得到了想要的结果:

Hello, World!
Factorial 0! = 1
Factorial 1! = 1
Factorial 2! = 2
Factorial 3! = 6
Factorial 6! = 720
Factorial 10! = 3628800

Process finished with exit code 0

当我运行测试目标时,我也得到了想要的结果:

===============================================================================
All tests passed (5 assertions in 1 test case)


Process finished with exit code 0

一切正常,我只是不喜欢我目前的解决方案。

如何让我的目标更容易扩展?

另外,附带问题: 包含头库的更合适的方法是什么?我认为它应该在 add_executable() 中,就像我在 /test/CMakeLists.txt 中所做的那样 ../include/catch2/catch.hpp...

【问题讨论】:

    标签: c++ unit-testing cmake catch-unit-test


    【解决方案1】:

    我想我在侧边栏的相关问题列表中找到了我的问题的答案:cmake project structure with unit tests

    使用add_library,我可以创建一个文件列表,其中包含不一定包含主文件的新目标名称。以下是我更改子目录 CMakeList.txt 文件的方式:

    src/CMakeList.txt

    add_library(src
            myMath/factorial.cpp myMath/factorial.h
    )
    
    add_executable(catch2Test main.cpp)
    
    target_link_libraries(catch2Test src)
    

    test/CMakeList.txt

    add_executable(tests
            ../include/catch2/catch.hpp
    
            test_main.cpp
            test_factorial.cpp
    )
    
    target_link_libraries(tests src)
    

    现在我添加的所有源文件都进入src 库,我的两个目标都使用target_link_libraries([target] src) 链接到它。

    现在我正在尝试确定是否可以使用 target_link_libraries 将 catch2 库附加到测试目标。

    编辑:我想我在这里找到了我对第二个问题的答案:'cmake os x failure ar no archive members specific' 和这里:'how to create a cmake header only library that depends on external header files'。

    我试图用单个头文件创建一个库,但 cmake 显然无法识别头文件。现在我的/include 文件夹中有一个CMakeLists.txt,如下所示:

    add_library(catch2 INTERFACE)
    target_sources(catch2 INTERFACE catch2/catch.hpp)
    

    我已将/include 文件夹添加到我的主CMakeLists.txt 文件中添加的子目录列表中。

    此外,我的测试目标现在使用 target_link_libraries(tests catch2) 链接 catch2 库,我发现这比将紧张的目录链接放在测试目标的可执行文件列表中要好得多。

    【讨论】:

      猜你喜欢
      • 2019-08-09
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多