4.2 使用Catch2库进行单元测试

NOTE:此示例代码可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-04/recipe-02 中找到,包含一个C++的示例。该示例在CMake 3.5版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。

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

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

准备工作

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

  1. #include "sum_integers.hpp"
  2. // this tells catch to provide a main()
  3. // only do this in one cpp file
  4. #define CATCH_CONFIG_MAIN
  5. #include "catch.hpp"
  6. #include <vector>
  7. TEST_CASE("Sum of integers for a short vector", "[short]")
  8. {
  9. auto integers = {1, 2, 3, 4, 5};
  10. REQUIRE(sum_integers(integers) == 15);
  11. }
  12. TEST_CASE("Sum of integers for a longer vector", "[long]")
  13. {
  14. std::vector<int> integers;
  15. for (int i = 1; i < 1001; ++i)
  16. {
  17. integers.push_back(i);
  18. }
  19. REQUIRE(sum_integers(integers) == 500500);
  20. }

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

具体实施

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

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

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

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

    1. $ mkdir -p build
    2. $ cd build
    3. $ cmake ..
    4. $ cmake --build .
    5. $ ctest -V
    6. UpdateCTestConfiguration from :/home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/DartConfiguration.tcl
    7. UpdateCTestConfiguration from :/home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/DartConfiguration.tcl
    8. Test project /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build
    9. Constructing a list of tests
    10. Done constructing a list of tests
    11. Updating test list for fixtures
    12. Added 0 tests to meet fixture requirements
    13. Checking test dependency graph...
    14. Checking test dependency graph end
    15. test 1
    16. Start 1: catch_test
    17. 1: Test command: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/build/cpp_test "--success"
    18. 1: Test timeout computed to be: 10000000
    19. 1:
    20. 1: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    21. 1: cpp_test is a Catch v2.0.1 host application.
    22. 1: Run with -? for options
    23. 1:
    24. 1: ----------------------------------------------------------------
    25. 1: Sum of integers for a short vector
    26. 1: ----------------------------------------------------------------
    27. 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:10
    28. 1: ...................................................................
    29. 1:
    30. 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:12:
    31. 1: PASSED:
    32. 1: REQUIRE( sum_integers(integers) == 15 )
    33. 1: with expansion:
    34. 1: 15 == 15
    35. 1:
    36. 1: ----------------------------------------------------------------
    37. 1: Sum of integers for a longer vector
    38. 1: ----------------------------------------------------------------
    39. 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:15
    40. 1: ...................................................................
    41. 1:
    42. 1: /home/user/cmake-cookbook/chapter-04/recipe-02/cxx-example/test.cpp:20:
    43. 1: PASSED:
    44. 1: REQUIRE( sum_integers(integers) == 500500 )
    45. 1: with expansion:
    46. 1: 500500 (0x7a314) == 500500 (0x7a314)
    47. 1:
    48. 1: ===================================================================
    49. 1: All tests passed (2 assertions in 2 test cases)
    50. 1:
    51. 1/1 Test #1: catch_test ....................... Passed 0.00 s
    52. 100% tests passed, 0 tests failed out of 1
    53. Total Test time (real) = 0.00 se
  4. 我们也可以测试cpp_test的二进制文件,可以直接从Catch2中看到输出:

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

    1. $ ./cpp_test --help

工作原理

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

  1. # Prepare "Catch" library for other executables
  2. set(CATCH_INCLUDE_DIR
  3. ${CMAKE_CURRENT_SOURCE_DIR}/catch)
  4. add_library(Catch
  5. INTERFACE)
  6. target_include_directories(Catch INTERFACE
  7. ${CATCH_INCLUDE_DIR})

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

  1. target_link_libraries(cpp_test Catch)

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

更多信息

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

Catch2代码库包含有CMake函数,用于解析Catch测试并自动创建CMake测试,不需要显式地输入add_test()函数,可见 https://github.com/catchorg/Catch2/blob/master/contrib/ParseAndAddCatchTests.cmake