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

Declaring and organizing tests
PrevUpHomeNext

If you look at many legacy test modules, big chance is that it's implemented as one big test function that consists of a mixture of check and output statements. Is there anything wrong with it? Yes. There are various disadvantages in single test function approach:

  • One big function tends to become really difficult to manage if the number of checks exceeds a reasonable limit (true for any large function). What is tested and where - who knows?
  • Many checks require similar preparations. This results in code repetitions within the test function.
  • If a fatal error or an exception is caused by any checks within the test function the rest of tests are skipped and there is no way to prevent this.
  • No way to perform only checks for a particular subsystem of the tested unit.
  • No summary of how different subsystems of the tested unit performed under in the test.

The above points should make it clear that it's preferable to split a test module into smaller units. These units are the test cases, the test suites and the fixtures.

Subjects covered by this section

Declaration

The Unit Test Framework supports several methods for declaring a test case. Test cases can be implemented using free function like syntax or based on actual free function, function object, that can be defined with or without parameters/data, or as template functions to be run against various types.

Organization

The Unit Test Framework provides facilities to group several test cases into test suites. The test suites can be nested, and the set of test suites and test cases defines the test tree, where the leaves are the test cases. Besides hierarchical structure the Unit Test Framework allows you to organize the test tree using logical grouping and dependencies and provides you with controls to utilize the defined test tree organization the way you want (eg. from command line).

Attributes

It is possible to specify test unit attributes by using decorators. Attributes are used for a fine grained control over various aspects of test module execution, such as logical grouping, dependencies, expected failures, etc.

Setup/teardown test unit actions

When several tests shares the same set-up (environment, test data preparation, etc.), the preparation and cleanup code may be factorized in fixtures. In the Unit Test Framework, fixtures can be associated to test cases, test suites or globally to the test module.


PrevUpHomeNext