Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost-test initialization for each suite (not case)

I need to init some variables, which are "global" inside a BOOST_AUTO_TEST_SUITE so their constructors will be called when the suite starts and their destructors will be called right after the last corresponding BOOST_AUTO_TEST_CASE is finished

does someone know how I can do it? Looks like global fixtures is not a solution...

like image 862
Alek86 Avatar asked Dec 14 '11 19:12

Alek86


3 Answers

I'm not quite sure if the accepted answer is correct, because if I use the test code from the boost web site:

#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>

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()      

Then the expected call sequence should be:

setup fixture
test_case1
test_case2
teardown fixture

But in fact it is this:

setup fixture
test_case1
teardown fixture
setup fixture
test_case2
teardown fixture

I don't know if this is a bug, because from reading the BOOST_FIXTURE_TEST_SUITE documentation, I would expect exactly the first behavior. I can also get the second behavior if I use BOOST_FIXTURE_TEST_CASE.

like image 126
DaRich Avatar answered Nov 20 '22 21:11

DaRich


For future reference:

This has been added to the library, as of 1.36 I believe.

like image 1
Sveltely Avatar answered Nov 20 '22 21:11

Sveltely


I don't think it's possible with the Boost Test Library. Global fixtures are really global, i.e. they are instantiated per test run, not per suite.

Apart from that, I think that such a setup would violate test isolation principles. Can you explain why you need "global" variables at the suite scope?

like image 1
Andre Avatar answered Nov 20 '22 21:11

Andre