简体   繁体   English

是否可以只初始化一次夹具并在多个测试用例中使用它?

[英]Is it possible to initialize the fixture only once and use it in multiple test cases?

Is it possible to have the fixture initialized only once and use it in multiple test cases within the same test suite?是否可以只初始化一次夹具并在同一测试套件中的多个测试用例中使用它? In the following example, fixture is constructed and destructed multiple times:在下面的示例中,fixture 被多次构造和销毁:

struct F {
    F() : i( 0 ) { BOOST_TEST_MESSAGE( "setup fixture" ); }
    ~F()         { BOOST_TEST_MESSAGE( "teardown fixture" ); }

    int i;
};


BOOST_FIXTURE_TEST_SUITE( s, F )

BOOST_AUTO_TEST_CASE( test_case1 )
{
    BOOST_CHECK( i == 1 );
}

BOOST_AUTO_TEST_CASE( test_case2 )
{
    BOOST_CHECK_EQUAL( i, 0 );
}

BOOST_AUTO_TEST_SUITE_END()

But I want the fixture to be constructed only once as the test suite begins and shared among all the test cases within it.但是我希望在测试套件开始时只构建一次夹具,并在其中的所有测试用例之间共享。 Is it possible?是否可以? The destructor would be called after exiting the test suite.退出测试套件后将调用析构函数。
I am using Boost Test Framework but have no problem using other frameworks like UnitTest++.我正在使用 Boost 测试框架,但使用其他框架(如 UnitTest++)没有问题。

Each test case is derived from the Test Suite Fixture, which is constructed at the start of each test case and destructed when it completes (in your case both test_case1 & test_case2 are derived from F ). 每个测试用例都来自于Test Suite Fixture,它是在每个测试用例的开始处构建的,并在完成时被销毁(在您的情况下, test_case1test_case2都从F派生)。 The fixture configures and cleans up the environment for each individual test case. 该夹具为每个单独的测试用例配置并清理环境。

For unit-testing this is usually the preferred strategy - each test case is stand alone and completely atomic. 对于单元测试,这通常是首选的策略-每个测试用例都是独立的,并且是完全原子的。

In some scenarios (eg integration testing ) it might be preferable to acquire an expensive resource once and hold onto it over all test cases. 在某些情况下(例如集成测试),可能最好一次获取一个昂贵的资源并在所有测试用例中保留它。 This can be done via a GLOBAL FIXTURE, which is constructed at the start of the test run and destructed when the test exits. 这可以通过GLOBAL FIXTURE来完成,它在测试运行开始时构建,并在测试退出时销毁。

If any test cases require a different setup / configuration of the global resources then a GLOBAL FIXTURE cannot be used and you should re-think your testing strategy so that each test case configures and cleans up its own environment. 如果任何测试用例需要对全局资源进行不同的设置/配置,则无法使用GLOBAL FIXTURE,您应该重新考虑测试策略,以便每个测试用例都可以配置和清理其自身的环境。

Unfortunately test cases do not have direct access to the global test fixture, ergo you will need to provide a mechanism that allows them to access the resource (eg via a global variable or singleton). 不幸的是,测试用例无法直接访问全局测试夹具,因此,您将需要提供一种机制,使它们可以访问资源(例如,通过全局变量或单例)。

In the example below MyFixture is a singleton that holds the resource. 在下面的示例中, MyFixture是容纳资源的单例。 eg 例如

struct MyFixture
{
    static MyFixture*& instance() { static MyFixture* s_inst = 0;
    return s_inst; }

    MyFixture()
    {
        instance() = this;
        x = 10;
        BOOST_TEST_MESSAGE( "setup fixture" );
    }

    ~MyFixture()
    {
        BOOST_TEST_MESSAGE( "teardown fixture" );
    }

    int x;
};

BOOST_GLOBAL_FIXTURE(MyFixture)


BOOST_AUTO_TEST_CASE(TEST_1)
{
    BOOST_CHECK(MyFixture::instance()->x == 10);
    MyFixture::instance()->x = 12;
}
BOOST_AUTO_TEST_CASE(TEST_2)
{
    BOOST_CHECK(MyFixture::instance()->x == 12);
}

It is possible but it requires you to use BOOST_AUTO_TEST_SUITE together with the boost::unit_test::fixture decorator instead of BOOST_FIXTURE_TEST_SUITE.这是可能的,但它要求您将 BOOST_AUTO_TEST_SUITE 与 boost::unit_test::fixture 装饰器一起使用,而不是 BOOST_FIXTURE_TEST_SUITE。 IT is also worth noting that while this will work you will not be able to access the member variables of F inside your test cases.还值得注意的是,虽然这会起作用,但您将无法访问测试用例中 F 的成员变量。 Tip: Just put them outside the struct instead like I do below with i.提示:只需将它们放在结构之外,而不是像我在下面对 i 所做的那样。 See here and here for more information in the boost documentation.有关升压文档中的更多信息,请参见此处此处

struct F {
F()  { BOOST_TEST_MESSAGE( "setup fixture" ); }
~F() { BOOST_TEST_MESSAGE( "teardown fixture" ); }
};

int i = 0;

BOOST_AUTO_TEST_SUITE( s, *boost::unit_test::fixture<F>())

BOOST_AUTO_TEST_CASE( test_case1 )
{
    BOOST_CHECK( i == 1 );
}

BOOST_AUTO_TEST_CASE( test_case2 )
{
    BOOST_CHECK_EQUAL( i, 0 );
}

BOOST_AUTO_TEST_SUITE_END()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM