Boost.Test:寻找一个有效的非平凡测试套件示例/教程 [英] Boost.Test: Looking for a working non-Trivial Test Suite Example / Tutorial

查看:26
本文介绍了Boost.Test:寻找一个有效的非平凡测试套件示例/教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Boost.Test 文档 和示例似乎并没有真正包含任何重要的示例,到目前为止我找到的两个教程 此处此处 虽然有帮助,但都是相当基础的.

The Boost.Test documentation and examples don't really seem to contain any non-trivial examples and so far the two tutorials I've found here and here while helpful are both fairly basic.

我想要一个用于整个项目的主测试套件,同时维护可以独立运行的单元测试和装置的每个模块套件.我还将使用模拟服务器来测试各种网络边缘情况.

I would like to have a master test suite for the entire project, while maintaining per module suites of unit tests and fixtures that can be run independently. I'll also be using a mock server to test various networking edge cases.

我使用的是 Ubuntu 8.04,但我会以 Linux 或 Windows 为例,因为我正在编写自己的 makefile.

I'm on Ubuntu 8.04, but I'll take any example Linux or Windows since I'm writing my own makefiles anyways.

编辑

作为测试,我做了以下事情:

As a test I did the following:

// test1.cpp
#define BOOST_TEST_MODULE Regression
#include <boost/test/included/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(test1_suite)

BOOST_AUTO_TEST_CASE(Test1)
{
    BOOST_CHECK(2 < 1);
}

BOOST_AUTO_TEST_SUITE_END()

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

BOOST_AUTO_TEST_SUITE(test2_suite)

BOOST_AUTO_TEST_CASE(Test1)
{
    BOOST_CHECK(1<2);
}

BOOST_AUTO_TEST_SUITE_END()

然后我编译它:g++ test1.cpp test2.cpp -o tests

这给了我关于链接过程中大量多重定义"错误的信息.

This gives me about a bazillion "multiple definition of" errors during linking.

当所有内容都在一个文件中时,它工作正常.

When it's all in a single file it works fine.

推荐答案

使用 Boost.Test 进行 C++ 单元测试

(永久链接:http://web.archive.org/web/20160524135412/http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/)

以上是一篇精彩的文章,比实际的 Boost 文档要好.

The above is a brilliant article and better than the actual Boost documentation.

我还写了一个 Perl 脚本,它将自动生成 makefile 和项目来自类名列表的骨架,包括一体机"测试套件和一个独立的测试套件每个班级.它被称为makeSimple 可以下载来自 Sourceforge.net.

I also wrote a Perl script which will auto-generate the makefile and project skeleton from a list of class names, including both the "all-in-one" test suite and a stand alone test suite for each class. It's called makeSimple and can be downloaded from Sourceforge.net.

我发现的基本问题是,如果您想将测试拆分为多个文件,您必须链接到预编译的测试运行时,而不是使用 Boost.Test 的仅标头"版本.您必须将 #define BOOST_TEST_DYN_LINK 添加到每个文件中,并且在包含 Boost 标头时,例如使用 <boost/test/unit_test.hpp> 而不是 <boost/test/included/unit_test.hpp>.

What I found to be the basic problem is that if you want to split your tests into multiple files you have to link against the pre-compiled test runtime and not use the "headers only" version of Boost.Test. You have to add #define BOOST_TEST_DYN_LINK to each file and when including the Boost headers for example use <boost/test/unit_test.hpp> instead of <boost/test/included/unit_test.hpp>.

所以要编译为单个测试:

So to compile as a single test:

g++ test_main.cpp test1.cpp test2.cpp -lboost_unit_test_framework -o tests

或编译单个测试:

g++ test1.cpp -DSTAND_ALONE -lboost_unit_test_framework -o test1

.

// test_main.cpp
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>

// test1.cpp
#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE Main
#endif
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(test1_suite)

BOOST_AUTO_TEST_CASE(Test1)
{
    BOOST_CHECK(2<1);
}

BOOST_AUTO_TEST_SUITE_END()

// test2.cpp
#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE Main
#endif
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(test2_suite)

BOOST_AUTO_TEST_CASE(Test1)
{
    BOOST_CHECK(1<2);
}

BOOST_AUTO_TEST_SUITE_END()

这篇关于Boost.Test:寻找一个有效的非平凡测试套件示例/教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆