Boost Test入口/出口夹具,带手动登记



我使用的是Boost 1.61,我正在使用以下类型的Boost Test设置,我手动注册测试:

// testsuite.cpp
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite(int, char* []) {
    test_suite* test = BOOST_TEST_SUITE("TestSuiteName");
    test->add(FooTest::suite());
    return test;
}
// foo.hpp
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
class FooTest {
public:
    static void testFoo1();
    static void testFoo2();
    static test_suite* suite();
}
// foo.cpp
#include <foo.hpp>
void FooTest::testFoo1() {
    // testFoo1 implementation
}
void FooTest::testFoo2() {
    // testFoo2 implementation
}
test_suite* FooTest::suite() {
    test_suite* suite = BOOST_TEST_SUITE("FooTest");
    suite->add(BOOST_TEST_CASE(&FooTest::testFoo1));
    suite->add(BOOST_TEST_CASE(&FooTest::testFoo2));
    return suite;
}

我想向FooTest测试套件添加一个入口/出口固定装置,如这里所述。Boost Test文档在自动注册测试的上下文中描述了此功能。

我可以在手动注册测试的设置中使用这个进入/退出固定装置功能吗?如果没有,是否有人建议我如何模仿这种行为,即在进入FooTest测试套件时构建一次对象,该对象可用于测试套件中的所有测试,然后在退出FooTest套件时销毁?

这里也有人问过类似的问题,但我找不到可以使用的答案。

为了防止它对某人有用,通过查看一些Boost Test源文件,我提出了以下似乎符合我的要求的内容,即使用手动注册的入口/出口夹具。

// testsuite.cpp
#include "foo.hpp"
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite(int, char*[]) {
    test_suite* test = BOOST_TEST_SUITE("TestSuiteName");
    test->add(FooTest::suite());
    return test;
}
// bar.hpp (The class to be tested)
class Bar {
public:
    Bar(int x, int y) : x_(x), y_(y) {}
    int x_;
    int y_;
};
// foo.hpp
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
class FooTest {
public:
    static void testFoo1();
    static void testFoo2();
    static test_suite* suite();
};
// foo.cpp
#include "foo.hpp"
#include "bar.hpp"
#include <boost/make_shared.hpp>
#include <vector>
using boost::unit_test::test_unit_fixture_ptr;
using std::vector;
namespace {
boost::shared_ptr<Bar> bar;
void setup() {
    BOOST_TEST_MESSAGE("Creating Bar Instance");
    bar = boost::make_shared<Bar>(1, 2);
}
void teardown() {
    BOOST_TEST_MESSAGE("Destroying Bar Instance");
    bar.reset();
}
}
void FooTest::testFoo1() {
    BOOST_TEST_MESSAGE("In testFoo1");
    BOOST_CHECK_MESSAGE(bar->x_ == 1, "Checking x_");
}
void FooTest::testFoo2() {
    BOOST_TEST_MESSAGE("In testFoo2");
    BOOST_CHECK_MESSAGE(bar->y_ == 2, "Checking y_");
}
test_suite* FooTest::suite() {
    test_suite* suite = BOOST_TEST_SUITE("FooTest");
    // Manually add an entry/exit fixture to the test suite
    vector<test_unit_fixture_ptr> fixtures {
        boost::make_shared<boost::unit_test::function_based_fixture>(setup, teardown)};
    suite->p_fixtures.set(fixtures);
    suite->add(BOOST_TEST_CASE(&FooTest::testFoo1));
    suite->add(BOOST_TEST_CASE(&FooTest::testFoo2));
    return suite;
}

当使用log_level=message 运行时,此代码给出以下输出

 Running 2 test cases...
 Creating Bar Instance
 In testFoo1
 In testFoo2
 Destroying Bar Instance
 *** No errors detected

如果你认为这种方法有缺陷,或者你觉得有更好的方法来实现我想要的,请告诉我。

最新更新