使用自由函数增强单元测试框架夹具

时间:2016-08-02 22:34:07

标签: boost boost-unit-test-framework

如何使用固定装置(设置和拆卸)的免费功能,如下所示:flexible models?文档没有显示示例,库测试不使用此方案。我正在寻找一个测试套件的例子。

1 个答案:

答案 0 :(得分:1)

FWIW,这对我有用:

#define BOOST_TEST_MODULE foo

#include <boost/test/included/unit_test.hpp>
namespace utf = boost::unit_test;

void setup ()    { BOOST_TEST_MESSAGE("set up fun"); }
void teardown () { BOOST_TEST_MESSAGE("tear down fun"); }

BOOST_AUTO_TEST_SUITE(bar, *utf::fixture (&setup, &teardown))

BOOST_AUTO_TEST_CASE(test1) {
    BOOST_TEST_MESSAGE("running test1");
    BOOST_TEST(true);
}

BOOST_AUTO_TEST_CASE(test2) {
    BOOST_TEST_MESSAGE("running test2");
    BOOST_TEST(true);
}

BOOST_AUTO_TEST_SUITE_END()

运行:

$ clang++ -I/usr/local/include test.cpp && ./a.out --log_level=all
Running 2 test cases...
Entering test module "foo"
test.cpp:9: Entering test suite "bar"
set up fun
test.cpp:11: Entering test case "test1"
running test1
test.cpp:13: info: check true has passed
test.cpp:11: Leaving test case "test1"; testing time: 56us
test.cpp:16: Entering test case "test2"
running test2
test.cpp:18: info: check true has passed
test.cpp:16: Leaving test case "test2"; testing time: 36us
tear down fun
test.cpp:9: Leaving test suite "bar"; testing time: 148us
Leaving test module "foo"; testing time: 244us
...

注意在套件运行之前调用setup,最后调用teardown。