Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.

fixture (decorator)

fixture(const boost::function<void()>& setup, const boost::function<void()>& teardown = {});

template <typename Fx>

template <typename Fx, typename Arg>
  fixture<Fx>(const Arg& arg);

Decorator fixture specifies a pair of functions (like set_up and tear_down) to be called before and after the corresponding test unit. At the suite level the set_up function is called once -- before the suite execution starts and tear_down function is called once -- after the suite execution ends. It comes in three forms. First expects two functions for set-up and tear-down (the second one can be skipped). The second expects a DefaultConstructible class. Its default constructor will be used as set-up function and its destructor as a tear-down function. Third requires a class with one-argument public constructor. Argument arg is forwarded to the constructor and this is the set-up function, its destructor is the tear-down function. There is no way to get access to the members of these fixtures from within the test case or test suite.

Example: decorator fixture


#define BOOST_TEST_MODULE decorator_12
#include <boost/test/included/unit_test.hpp>
namespace utf = boost::unit_test;

struct Fx
  std::string s;
  Fx(std::string s = "") : s(s)
        { BOOST_TEST_MESSAGE("set up " << s); }
  ~Fx() { BOOST_TEST_MESSAGE("tear down " << s); }

void setup() { BOOST_TEST_MESSAGE("set up fun"); }
void teardown() { BOOST_TEST_MESSAGE("tear down fun"); }

  * utf::fixture<Fx>(std::string("FX"))
  * utf::fixture<Fx>(std::string("FX2")))

  BOOST_AUTO_TEST_CASE(test1, * utf::fixture(&setup, &teardown))
    BOOST_TEST_MESSAGE("running test1");

    BOOST_TEST_MESSAGE("running test2");



> decorator_12 --log_level=message
Running 2 test cases...
set up FX
set up FX2
set up fun
running test1
tear down fun
running test2
tear down FX2
tear down FX

*** No errors detected

For other ways of using fixtures, see here.