...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
Several fixture interfaces are supported by the Unit Test Framework. The choice of the interface depends mainly on the usage of the fixture.
The Unit Test Framework defines the generic fixture class model as follows:
struct <fixture-name>{ <fixture-name>(); // setup function ~<fixture-name>(); // teardown function };
In other words a fixture is expected to be implemented as a class where
the class constructor serves as a setup
method and class destructor serves as teardown
method.
The class model above has some limitations though:
This is why the Unit Test Framework also supports
(Boost 1.65 on) optional setup
and/or teardown
functions
as follow:
struct <fixture-name>{ <fixture-name>(); // ctor ~<fixture-name>(); // dtor void setup(); // setup, optional void teardown(); // teardown, optional };
Note | |
---|---|
As mentioned, the declaration/implementation of the |
This model is expected from fixtures used with BOOST_FIXTURE_TEST_CASE
and BOOST_FIXTURE_TEST_SUITE
.
In addition to BOOST_FIXTURE_TEST_CASE
and BOOST_FIXTURE_TEST_SUITE
the
Unit Test Framework allows to associate fixture with
test unit using the decorator fixture
. This decorator supports
additional models for declaring the setup
and teardown
:
a fixture defined according to the extended fixture class model, which allows for the fixture constructor to takes one argument. For example:
struct Fx { std::string s; Fx(std::string s_ = "") : s(s_) { BOOST_TEST_MESSAGE("ctor " << s); } void setup() { BOOST_TEST_MESSAGE("optional setup " << s); } void teardown() { BOOST_TEST_MESSAGE("optional teardown " << s); } ~Fx() { BOOST_TEST_MESSAGE("dtor " << s); } };
a fixture defined as a pair of free functions for the setup
and teardown
(latter optional)
void setup() { BOOST_TEST_MESSAGE("set up"); } void teardown() { BOOST_TEST_MESSAGE("tear down"); }
For complete example of test module which uses these models please check
decorator fixture
.