Home > The Unit Test Framework > User's guide > Fixtures > Test suite shared
PrevNext

Test suite level fixture

If all test cases in a test require the same fixture (you can group test cases in the test suite based on a fixture required) you can make another step toward an automation of a test fixture assignment. To assign the same shared fixture for all test cases in a test suite use the macro BOOST_FIXTURE_TEST_SUITE in place of the macro BOOST_AUTO_TEST_SUITE for automated test suite creation and registration.

BOOST_FIXTURE_TEST_SUITE(suite_name, fixure_name)

Once again the only difference from the macro BOOST_AUTO_TEST_SUITE usage is the presence of an extra argument - the fixture name. And now, you not only have direct access to the public and protected members of the fixture, but also do not need to refer to the fixture name in test case definition. All test cases assigned the same fixture automatically.

[Tip] Tip

If necessary you can reset the fixture for a particular test case with the use of the macro BOOST_FIXTURE_TEST_CASE.

[Note] Note

The fixture assignment is "deep". In other words unless reset by another BOOST_FIXTURE_TEST_SUITE or BOOST_FIXTURE_TEST_CASE definition the same fixture is assigned to all test cases, including ones that belong to the sub test suites.

Example24.Test suite level fixture

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

Source code | Show output
> example
Running 2 test cases...                                                         
test.cpp(17): error in "test_case1": check i == 1 failed

*** 1 failure detected in test suite "example"


PrevUpHomeNext