0

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;
}

hereで説明されているように、FooTest テスト スイートにエントリ/エグジット フィクスチャを追加したいと考えています。Boost Test のドキュメントでは、自動登録されたテストのコンテキストでこの機能について説明しています。

テストを手動で登録しているセットアップで、このエントリ/エグジット フィクスチャ機能を使用できますか? そうでない場合、この動作をどのように模倣できるかについて誰か提案がありますか?

同様の質問がここで尋ねられましたが、使用できる答えが見つかりませんでした。

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 に答える