草庐IT

c++ - Boost.Test : Looking for a working non-Trivial Test Suite Example/Tutorial

coder 2023-05-02 原文

Boost.Test documentation并且示例似乎并没有真正包含任何重要的示例,到目前为止,我发现的两个教程 herehere虽然有帮助,但都是相当基本的。

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

我使用的是 Ubuntu 8.04,但我会以任何 Linux 或 Windows 为例,因为无论如何我都在编写自己的 makefile。

编辑

作为测试,我做了以下操作:

// 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

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

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

最佳答案

C++ Unit Testing With Boost.Test

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

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

编辑:

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 的“仅 header ”版本。您必须添加 #define BOOST_TEST_DYN_LINK到每个文件,例如在包含 Boost header 时使用 <boost/test/unit_test.hpp>而不是 <boost/test/included/unit_test.hpp> .

所以编译为单个测试:

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()

关于c++ - Boost.Test : Looking for a working non-Trivial Test Suite Example/Tutorial,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2906095/

有关c++ - Boost.Test : Looking for a working non-Trivial Test Suite Example/Tutorial的更多相关文章

随机推荐