Boost C++ Libraries

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

Fixture models
PrevUpHomeNext

Several fixture interfaces are supported by the Unit Test Framework. The choice of the interface depends mainly on the usage of the fixture.

Fixture class model

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:

  • it is not possible to have exceptions in the teardown function, especially any test assertions that aborts the current test case is not possible (as those use exceptions)
  • it is sometimes more natural to use the constructor/destructor to perform the necessary resource allocation/release of the fixture, and that will be consumed in the test cases, and check for the proper state of the fixture in separate functions. Those checks are the pre-conditions for the test case to run, and the post-conditions that should be met after the test case has been running.

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] Note

As mentioned, the declaration/implementation of the setup and teardown are optional: the Unit Test Framework will check the existence of those and will call them adequately. However in C++98, it is not possible to detect those declaration in case those are inherited (it works fine for compiler supporting auto and decltype).

This model is expected from fixtures used with BOOST_FIXTURE_TEST_CASE and BOOST_FIXTURE_TEST_SUITE.

Flexible models

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 fixture class model above
  • 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 setup()
          { 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.


PrevUpHomeNext