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

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 代替 BOOST_AUTO_TEST_SUITE 來完成測試套件的自動創建和註冊。

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.
和宏 BOOST_AUTO_TEST_SUITE 的唯一區別就是額外的參數 - 夾具名稱。 現在,你不僅可以直接訪問夾具的公共和保護成員,並且不需要在測試用例定義中指定夾具名稱。 所有的測試用例都自動指定相同的夾具。

[Tip] Tip

If necessary you can reset the fixture for a particular test case with the use of the macro BOOST_FIXTURE_TEST_CASE.
如果需要,可以使用宏 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.
夾具的指定是 "深入" 的。換句話說,除非用另一個 BOOST_FIXTURE_TEST_SUITE 重新設置, 否則 BOOST_FIXTURE_TEST_CASE 定義的夾具會指定到所有的測試用例,包括子測試套件中。

Example 24. 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_AUTO_TEST_CASE( test_case1 )
    BOOST_CHECK( i == 1 );


BOOST_AUTO_TEST_CASE( test_case2 )
    BOOST_CHECK_EQUAL( i, 0 );



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"