c++ - Boost.Test - 问题覆盖主要

标签 c++ unit-testing boost boost-test

我想在使用 Boost.Test 时提供自己的主要功能。所以我包含了以下宏:

#define BOOST_TEST_ALTERNATIVE_INIT_API
#define BOOST_TEST_NO_MAIN

在我的主要功能中,我调用了:

::boost::unit_test::unit_test_main(&registering_all_tests, argc, argv)

但是当使用 Xcode 6 在 OS X 上构建时,这给了我以下错误:

Undefined symbols for architecture x86_64:
  "boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
      _main in main.o

我发现包括以下文件:

#include <boost/test/included/unit_test.hpp>

解决了这个问题,但我对其中的含义感到困惑。该文件的描述表明它使用包含(与链接)版本的单元测试框架。

Boost 入门指南指出:

Boost.Test can be used in “header-only” or “separately compiled” mode, although separate compilation is recommended for serious use.

我已经单独构建了库。

Boost 文档还指出:

Including the UTF directly into your test module

If you prefer to avoid the standalone library compilation you can either include all files that constitute the static library in your test module's makefile or include them as a part of a test module's source file. To facilitate the later variant the UTF presents the single-header usage variant. In either case no special build options or macro definitions are required to be added to your compilation options list by default. But the same flags that can be used for the standalone library compilation are applicable in this case. Though, obviously, neither BOOST_TEST_DYN_LINK nor BOOST_TEST_NO_LIB are applicable. This solution may not be the best choice in a long run, since it requires the UTF sources recompilation for every test module you use it with and for every change of a test module you are working on. In a result your testing cycle time may increase. If it become tiresome, I recommend switching to one of the prebuilt library usage variants.

看来,通过选择包含单元测试框架的包含(与链接)版本,我做错了事。

有人可以澄清正确的方法吗?覆盖 main 的示例没有提到需要这样做。

最佳答案

最新版本的Boost.Test documentation比以前的版本更详细地介绍了这个主题。

通过使用定义的符号 BOOST_TEST_NO_MAIN 构建单元测试框架静态库,然后我可以提供我自己的 main 函数,它使用默认初始化手动调用默认测试运行器 unit_test_main函数 init_unit_test 作为第一个参数:

int exitCode = ::boost::unit_test::unit_test_main(&init_unit_test_suite, argc, argv);

无需定义 BOOST_TEST_ALTERNATIVE_INIT_API

关于c++ - Boost.Test - 问题覆盖主要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32347214/

相关文章:

C++11 auto 和 void* 泛型

c++ - 如何使用 x64 运行 cl?

node.js - 如何在strapi框架内实现单元测试

android-ndk 构建无法链接 boost 系统

c++ - Boost.Serialization 和 Boost.Python 双向 pickle

c++ - 使用 Boost Fusion 显示扁平的 Phoenix 表达式

c++ - 意外的无限循环 (C++)

C++ 数组 : conversion of contained type

maven - 无法运行ElasticSearch测试

android - 使用 gradle 仅在一个构建变体上运行单元测试