boost-test initialization for each suite (not case)
Asked Answered
J

4

6

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...

Jamnes answered 14/12, 2011 at 19:10 Comment(1)
This is the test entry/exit fixture explained here: boost.org/doc/libs/1_70_0/libs/test/doc/html/boost_test/…Heterosexuality
V
1

For future reference:

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

Venitavenite answered 1/4, 2014 at 4:4 Comment(0)
V
10

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.

Velma answered 9/1, 2015 at 10:19 Comment(2)
My thoughts exactly.. Feels like the documentation is misguiding in this case. I'd love to know if anyone's found a solution for this.Phenomena
I would call this "desirable behaviour". You want to be able to run unit tests in a random order. If you didn't have a clean fixture each time, previous tests may affect this one. The point of a fixture is to delegate the setup before each test to simplify the tests themselves. It's certainly not a bug.Alfonzoalford
A
1

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?

Axilla answered 14/12, 2011 at 20:46 Comment(2)
initially I thought I would need it to open a connection to my DB and to close it as fast as possible. but after some time of thinking I start to believe I do not need to close it fast so much... Or just open-close for each case, not the whole suiteJamnes
Why not re-initialise / clear down after every test case? You could use a suite fixture to do this and place your init/cleardown code in the constructor / destructor, since each test case will be derived from the suite fixture you can ensure that each test case starts from a known state.Crepuscule
V
1

For future reference:

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

Venitavenite answered 1/4, 2014 at 4:4 Comment(0)
C
1

You can use global fixtures: http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/tests_organization/fixtures/global.html

just replace

BOOST_FIXTURE_TEST_SUITE( s, F )
BOOST_AUTO_TEST_CASE( test_case1 )
[...]
BOOST_AUTO_TEST_SUITE_END()

with

BOOST_TEST_GLOBAL_FIXTURE( F );
BOOST_AUTO_TEST_CASE( test_case1 )
[...]

Then it will work like you expect.

-- sym39

Crews answered 12/1, 2018 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.