0

我正在使用 Boost 1.61,并且正在使用以下类型的 Boost 测试设置,我在其中手动注册测试:

// 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 套件时被破坏?

此处提出了类似的问题,但我找不到可以使用的答案。

4

1 回答 1

0

以防万一它对某人有用,通过查看一些 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

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

于 2016-08-07T22:53:08.733 回答