Each test case is derived from the Test Suite Fixture, which is created at the beginning of each test case and is destroyed when it is completed (in your case, both test_case1 and test_case2 obtained from F ). Commit sets up and cleans up the environment for each individual test case.
For unit testing, this is usually the preferred strategy - each test case is autonomous and completely atomic.
In some scenarios (for example, to test integration), it may be preferable to get an expensive resource once and run it over all test cases. This can be done using GLOBAL FIXTURE, which is created at the beginning of the test run and destroyed when the test completes.
If any test cases require a different setting / configuration of global resources, then GLOBAL FIXTURE cannot be used, and you should revise your testing strategy so that each test case sets up and cleans its own environment.
Unfortunately, test cases do not have direct access to the global test equipment, and you will need to provide a mechanism that allows them to access the resource (for example, through a global variable or singleton).
In the example below, MyFixture is a singleton that contains a resource. eg.
struct MyFixture { static MyFixture*& instance() { static MyFixture* s_inst = 0; return s_inst; } MyFixture() { instance() = this; x = 10; BOOST_TEST_MESSAGE( "setup fixture" ); } ~MyFixture() { BOOST_TEST_MESSAGE( "teardown fixture" ); } int x; }; BOOST_GLOBAL_FIXTURE(MyFixture) BOOST_AUTO_TEST_CASE(TEST_1) { BOOST_CHECK(MyFixture::instance()->x == 10); MyFixture::instance()->x = 12; } BOOST_AUTO_TEST_CASE(TEST_2) { BOOST_CHECK(MyFixture::instance()->x == 12); }
mark
source share