# 4.4 使用Boost Test进行单元测试

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

Boost Test是在C++社区中，一个非常流行的单元测试框架。本例中，我们将演示如何使用Boost Test，对求和示例代码进行单元测试。

## 准备工作

`main.cpp`、`sum_integers.cpp`和`sum_integers.hpp`与之前的示例相同，将更新`test.cpp`作为使用Boost Test库进行的单元测试：

```cpp
#include "sum_integers.hpp"

#include <vector>

#define BOOST_TEST_MODULE example_test_suite
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(add_example)
{
  auto integers = {1, 2, 3, 4, 5};
  auto result = sum_integers(integers);
  BOOST_REQUIRE(result == 15);
}
```

## 具体实施

以下是使用Boost Test构建项目的步骤:

1. 先从`CMakeLists.txt`开始:

   ```
   # set minimum cmake version
   cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

   # project name and language
   project(recipe-04 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)
   ```
2. 检测Boost库并将`cpp_test`链接到它:

   ```
   find_package(Boost 1.54 REQUIRED COMPONENTS unit_test_framework)

   add_executable(cpp_test test.cpp)

   target_link_libraries(cpp_test
     PRIVATE
       sum_integers
       Boost::unit_test_framework
     )

   # avoid undefined reference to "main" in test.cpp
   target_compile_definitions(cpp_test
     PRIVATE
         BOOST_TEST_DYN_LINK
     )
   ```
3. 最后，定义单元测试:

   ```
   enable_testing()

   add_test(
     NAME boost_test
     COMMAND $<TARGET_FILE:cpp_test>
     )
   ```
4. 下面是需要配置、构建和测试代码的所有内容:

   ```
   $ mkdir -p build
   $ cd build
   $ cmake ..
   $ cmake --build .
   $ ctest

   Test project /home/user/cmake-recipes/chapter-04/recipe-04/cxx-example/build
   Start 1: boost_test
   1/1 Test #1: boost_test ....................... Passed 0.01 sec
   100% tests passed, 0 tests failed out of 1
   Total Test time (real) = 0.01 sec

   $ ./cpp_test

   Running 1 test case...
   *** No errors detected
   ```

## 工作原理

使用`find_package`来检测Boost的`unit_test_framework`组件(参见第3章，第8节)。我们认为这个组件是`REQUIRED`的，如果在系统环境中找不到它，配置将停止。`cpp_test`目标需要知道在哪里可以找到Boost头文件，并且需要链接到相应的库；它们都由`IMPORTED`库目标`Boost::unit_test_framework`提供，该目标由`find_package`设置。

## 更多信息

本示例中，我们假设系统上安装了Boost。或者，我们可以在编译时获取并构建Boost依赖项。然而，Boost不是轻量级依赖项。我们的示例代码中，我们只使用了最基本的设施，但是Boost提供了丰富的特性和选项，有感兴趣的读者可以去这里看看：<http://www.boost.org/doc/libs/1_65_1/libs/test/doc/html/index.html> 。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chenxiaowei.gitbook.io/cmake-cookbook/4.0-chinese/4.4-chinese.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
