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

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

问题描述

借助 Boost.Test文档和例子真的不似乎包含任何非平凡的例子,到目前为止,这两个教程我发现这里和<一个href=\"http://us.generation-nt.com/tutorial-c-plus-plus-unit-tests-boost-test-help-12921932.html\">here而有益的都是非常基本的。

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,但因为我反正写我自己的makefile,我会采取任何例如Linux或Windows。

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 -o测试2.cpp测试

这给了我一个bazillion错误的多重定义链接中。

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

当这一切都在一个单独的文件,它工作正常。

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

推荐答案

C ++单元测试随着Boost.Test

以上是一个辉煌的文章,并优于实际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.

我认为是基本的问题是,如果你想你的测试分为多个文件,你必须对pre-编译测试运行时链接,而不是使用Boost.Test的头文件只能版。你必须添加的#define BOOST_TEST_DYN_LINK 来的每个文件,包括升压头时,例如使用&LT;升压/测试/ unit_test.hpp&GT; ,而不是&LT;升压/测试/包括/ unit_test.hpp方式&gt;

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天全站免登陆