Home > The Unit Test Framework > User's guide > Fixtures > Global fixture
PrevNext

Global fixture

Any global initialization that needs to be performed every time testing begins or a global cleanup that is to be performed once testing is finished is called a global fixture. The UTF global fixture design is based on a generic test fixture model and is supported by the utility class boost::unit_test::global_fixture. The global fixture design allows any number of global fixtures to be defined in any test file that constitutes a test module. Though some initialization can be implemented in the test module initialization function, there are several reasons to prefer the global fixture approach:
每次測試開始時需要的全局初始化或測試結束時需要的全局清理被稱為全局夾具。 UTF 的全局夾具設計基於通用測試夾具模型, 並通過工具類 boost::unit_test::global_fixture 支持。 全局夾具設計允許在測試模塊任何測試文件中定義任意數量的全局夾具。 雖然有些初始化可以在測試模塊初始化函數中實現,但全局夾具還是有意義的:

To define a global test module fixture you need to implement a class that matched generic fixture model and passed it as an argument to the macro BOOST_GLOBAL_FIXTURE.
要定義全局的測試模塊夾具,你需要實現一個符合通用夾具模型的類,並將其名稱作為宏 BOOST_GLOBAL_FIXTURE 的參數。

BOOST_GLOBAL_FIXTURE(fixure_name)

The statement, that performs global fixture definition, has to reside at a test file scope.
這個執行全局夾具定義的語句,應該位於一個測試文件範圍內。

Example 25. Global fixture

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

//____________________________________________________________________________//

struct MyConfig {
    MyConfig()   { std::cout << "global setup\n"; }
    ~MyConfig()  { std::cout << "global teardown\n"; }
};

//____________________________________________________________________________//

BOOST_GLOBAL_FIXTURE( MyConfig );

BOOST_AUTO_TEST_CASE( test_case )
{
    BOOST_CHECK( true );
}

//____________________________________________________________________________//

Source code | Show output
> example
global setup
Running 1 test case...
global teardown

*** No errors detected


PrevUpHomeNext