📘
CMake Cookbook
  • Introduction
  • 前言
  • 第0章 配置环境
    • 0.1 获取代码
    • 0.2 Docker镜像
    • 0.3 安装必要的软件
    • 0.4 测试环境
    • 0.5 上报问题并提出改进建议
  • 第1章 从可执行文件到库
    • 1.1 将单个源文件编译为可执行文件
    • 1.2 切换生成器
    • 1.3 构建和链接静态库和动态库
    • 1.4 用条件句控制编译
    • 1.5 向用户显示选项
    • 1.6 指定编译器
    • 1.7 切换构建类型
    • 1.8 设置编译器选项
    • 1.9 为语言设定标准
    • 1.10 使用控制流
  • 第2章 检测环境
    • 2.1 检测操作系统
    • 2.2 处理与平台相关的源代码
    • 2.3 处理与编译器相关的源代码
    • 2.4 检测处理器体系结构
    • 2.5 检测处理器指令集
    • 2.6 为Eigen库使能向量化
  • 第3章 检测外部库和程序
    • 3.1 检测Python解释器
    • 3.2 检测Python库
    • 3.3 检测Python模块和包
    • 3.4 检测BLAS和LAPACK数学库
    • 3.5 检测OpenMP的并行环境
    • 3.6 检测MPI的并行环境
    • 3.7 检测Eigen库
    • 3.8 检测Boost库
    • 3.9 检测外部库:Ⅰ. 使用pkg-config
    • 3.10 检测外部库:Ⅱ. 自定义find模块
  • 第4章 创建和运行测试
    • 4.1 创建一个简单的单元测试
    • 4.2 使用Catch2库进行单元测试
    • 4.3 使用Google Test库进行单元测试
    • 4.4 使用Boost Test进行单元测试
    • 4.5 使用动态分析来检测内存缺陷
    • 4.6 预期测试失败
    • 4.7 使用超时测试运行时间过长的测试
    • 4.8 并行测试
    • 4.9 运行测试子集
    • 4.10 使用测试固件
  • 第5章 配置时和构建时的操作
    • 5.1 使用平台无关的文件操作
    • 5.2 配置时运行自定义命令
    • 5.3 构建时运行自定义命令:Ⅰ. 使用add_custom_command
    • 5.4 构建时运行自定义命令:Ⅱ. 使用add_custom_target
    • 5.5 构建时为特定目标运行自定义命令
    • 5.6 探究编译和链接命令
    • 5.7 探究编译器标志命令
    • 5.8 探究可执行命令
    • 5.9 使用生成器表达式微调配置和编译
  • 第6章 生成源码
    • 6.1 配置时生成源码
    • 6.2 使用Python在配置时生成源码
    • 6.3 构建时使用Python生成源码
    • 6.4 记录项目版本信息以便报告
    • 6.5 从文件中记录项目版本
    • 6.6 配置时记录Git Hash值
    • 6.7 构建时记录Git Hash值
  • 第7章 构建项目
    • 7.1 使用函数和宏重用代码
    • 7.2 将CMake源代码分成模块
    • 7.3 编写函数来测试和设置编译器标志
    • 7.4 用指定参数定义函数或宏
    • 7.5 重新定义函数和宏
    • 7.6 使用废弃函数、宏和变量
    • 7.7 add_subdirectory的限定范围
    • 7.8 使用target_sources避免全局变量
    • 7.9 组织Fortran项目
  • 第8章 超级构建模式
    • 8.1 使用超级构建模式
    • 8.2 使用超级构建管理依赖项:Ⅰ.Boost库
    • 8.3 使用超级构建管理依赖项:Ⅱ.FFTW库
    • 8.4 使用超级构建管理依赖项:Ⅲ.Google Test框架
    • 8.5 使用超级构建支持项目
  • 第9章 语言混合项目
    • 9.1 使用C/C++库构建Fortran项目
    • 9.2 使用Fortran库构建C/C++项目
    • 9.3 使用Cython构建C++和Python项目
    • 9.4 使用Boost.Python构建C++和Python项目
    • 9.5 使用pybind11构建C++和Python项目
    • 9.6 使用Python CFFI混合C,C++,Fortran和Python
  • 第10章 编写安装程序
    • 10.1 安装项目
    • 10.2 生成输出头文件
    • 10.3 输出目标
    • 10.4 安装超级构建
  • 第11章 打包项目
    • 11.1 生成源代码和二进制包
    • 11.2 通过PyPI发布使用CMake/pybind11构建的C++/Python项目
    • 11.3 通过PyPI发布使用CMake/CFFI构建C/Fortran/Python项目
    • 11.4 以Conda包的形式发布一个简单的项目
    • 11.5 将Conda包作为依赖项发布给项目
  • 第12章 构建文档
    • 12.1 使用Doxygen构建文档
    • 12.2 使用Sphinx构建文档
    • 12.3 结合Doxygen和Sphinx
  • 第13章 选择生成器和交叉编译
    • 13.1 使用CMake构建Visual Studio 2017项目
    • 13.2 交叉编译hello world示例
    • 13.3 使用OpenMP并行化交叉编译Windows二进制文件
  • 第14章 测试面板
    • 14.1 将测试部署到CDash
    • 14.2 CDash显示测试覆盖率
    • 14.3 使用AddressSanifier向CDash报告内存缺陷
    • 14.4 使用ThreadSaniiser向CDash报告数据争用
  • 第15章 使用CMake构建已有项目
    • 15.1 如何开始迁移项目
    • 15.2 生成文件并编写平台检查
    • 15.3 检测所需的链接和依赖关系
    • 15.4 复制编译标志
    • 15.5 移植测试
    • 15.6 移植安装目标
    • 15.7 进一步迁移的措施
    • 15.8 项目转换为CMake的常见问题
  • 第16章 可能感兴趣的书
    • 16.1 留下评论——让其他读者知道你的想法
Powered by GitBook
On this page
  • 准备工作
  • 具体实施
  • 工作原理
  • 更多信息

Was this helpful?

  1. 第4章 创建和运行测试

4.2 使用Catch2库进行单元测试

Previous4.1 创建一个简单的单元测试Next4.3 使用Google Test库进行单元测试

Last updated 5 years ago

Was this helpful?

NOTE:此示例代码可以在 中找到,包含一个C++的示例。该示例在CMake 3.5版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。

前面的配置中,使用返回码来表示test.cpp测试的成功或失败。对于简单功能没问题,但是通常情况下,我们想要使用一个测试框架,它提供了相关基础设施来运行更复杂的测试,包括固定方式进行测试,与数值公差的比较,以及在测试失败时输出更好的错误报告。这里,我们用目前比较流行的测试库Catch2( )来进行演示。这个测试框架有个很好的特性,它可以通过单个头库包含在项目中进行测试,这使得编译和更新框架特别容易。这个配置中,我们将CMake和Catch2结合使用,来测试上一个求和代码。

我们需要catch.hpp头文件,可以从 (我们使用的是版本2.0.1)下载,并将它与test.cpp一起放在项目的根目录下。

准备工作

main.cpp、sum_integers.cpp和sum_integers.hpp与之前的示例相同,但将更新test.cpp:

#include "sum_integers.hpp"

// this tells catch to provide a main()
// only do this in one cpp file
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <vector>

TEST_CASE("Sum of integers for a short vector", "[short]")
{
  auto integers = {1, 2, 3, 4, 5};
  REQUIRE(sum_integers(integers) == 15);
}

TEST_CASE("Sum of integers for a longer vector", "[long]")
{
  std::vector<int> integers;
  for (int i = 1; i < 1001; ++i)
  {
    integers.push_back(i);
  }
  REQUIRE(sum_integers(integers) == 500500);
}

具体实施

使用Catch2库,需要修改之前的所使用CMakeList.txt:

  1. 保持CMakeLists.txt大多数部分内容不变:

    # set minimum cmake version
    cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
    
    # project name and language
    project(recipe-02 LANGUAGES CXX)
    
    # require C++11
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_EXTENSIONS OFF)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    # example library
    add_library(sum_integers sum_integers.cpp)
    
    # main code
    add_executable(sum_up main.cpp)
    target_link_libraries(sum_up sum_integers)
    
    # testing binary
    add_executable(cpp_test test.cpp)
    target_link_libraries(cpp_test sum_integers)
  2. 对于上一个示例的配置,需要保留一个测试,并重命名它。注意,--success选项可传递给单元测试的可执行文件。这是一个Catch2选项,测试成功时,也会有输出:

    enable_testing()
    
    add_test(
      NAME catch_test
      COMMAND $<TARGET_FILE:cpp_test> --success
      )
  3. 就是这样!让我们来配置、构建和测试。CTest中,使用-V选项运行测试,以获得单元测试可执行文件的输出:

    $ mkdir -p build
    $ cd build
    $ cmake ..
    $ cmake --build .
    $ ctest -V
    
    UpdateCTestConfiguration from :/home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/DartConfiguration.tcl
    UpdateCTestConfiguration from :/home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/DartConfiguration.tcl
    Test project /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build
    Constructing a list of tests
    Done constructing a list of tests
    Updating test list for fixtures
    Added 0 tests to meet fixture requirements
    Checking test dependency graph...
    Checking test dependency graph end
    test 1
    Start 1: catch_test
    1: Test command: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/cpp_test "--success"
    1: Test timeout computed to be: 10000000
    1:
    1: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1: cpp_test is a Catch v2.0.1 host application.
    1: Run with -? for options
    1:
    1: ----------------------------------------------------------------
    1: Sum of integers for a short vector
    1: ----------------------------------------------------------------
    1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:10
    1: ...................................................................
    1:
    1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:12:
    1: PASSED:
    1: REQUIRE( sum_integers(integers) == 15 )
    1: with expansion:
    1: 15 == 15
    1:
    1: ----------------------------------------------------------------
    1: Sum of integers for a longer vector
    1: ----------------------------------------------------------------
    1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:15
    1: ...................................................................
    1:
    1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:20:
    1: PASSED:
    1: REQUIRE( sum_integers(integers) == 500500 )
    1: with expansion:
    1: 500500 (0x7a314) == 500500 (0x7a314)
    1:
    1: ===================================================================
    1: All tests passed (2 assertions in 2 test cases)
    1:
    1/1 Test #1: catch_test ....................... Passed 0.00 s
    
    100% tests passed, 0 tests failed out of 1
    
    Total Test time (real) = 0.00 se
  4. 我们也可以测试cpp_test的二进制文件,可以直接从Catch2中看到输出:

    $ ./cpp_test --success
    
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    cpp_test is a Catch v2.0.1 host application.
    Run with -? for options
    -------------------------------------------------------------------
    Sum of integers for a short vector
    -------------------------------------------------------------------
    /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:10
    ...................................................................
    /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:12:
    PASSED:
    REQUIRE( sum_integers(integers) == 15 )
    with expansion:
    15 == 15
    -------------------------------------------------------------------
    Sum of integers for a longer vector
    -------------------------------------------------------------------
    /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:15
    ...................................................................
    /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:20:
    PASSED:
    REQUIRE( sum_integers(integers) == 500500 )
    with expansion:
    500500 (0x7a314) == 500500 (0x7a314)
    ===================================================================
    All tests passed (2 assertions in 2 test cases)
  5. Catch2将生成一个可执行文件,还可以尝试执行以下命令,以探索单元测试框架提供的选项:

    $ ./cpp_test --help

工作原理

# Prepare "Catch" library for other executables 
set(CATCH_INCLUDE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/catch) 

add_library(Catch
INTERFACE) 

target_include_directories(Catch INTERFACE
${CATCH_INCLUDE_DIR})

然后,我们对库进行如下链接:

target_link_libraries(cpp_test Catch)

回想一下第3中的讨论,在第1章从简单的可执行库到接口库,是CMake提供的伪目标库,这些伪目标库对于指定项目外部目标的需求非常有用。

更多信息

catch.hpp头文件可以从 (版本为2.0.1)下载,并将它与test.cpp放在项目的根目录中。

Catch2是一个单头文件测试框架,所以不需要定义和构建额外的目标。只需要确保CMake能找到catch.hpp,从而构建test.cpp即可。为了方便起见,将它放在与test.cpp相同的目录中,我们可以选择一个不同的位置,并使用target_include_directory指示该位置。另一种方法是将头部封装到接口库中,这可以在Catch2文档中说明( ):

这是一个简单的例子,主要关注CMake。当然,Catch2提供了更多功能。有关Catch2框架的完整文档,可访问 。

Catch2代码库包含有CMake函数,用于解析Catch测试并自动创建CMake测试,不需要显式地输入add_test()函数,可见 。

https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-04/recipe-02
https://github.com/catchorg/Catch2
https://github.com/catchorg/Catch2
https://github.com/catchorg/Catch2
https://github.com/catchorg/catch2/blob/maste/docs/build.systems.md#cmake
https://github.com/catchorg/Catch2
https://github.com/catchorg/Catch2/blob/master/contrib/ParseAndAddCatchTests.cmake