Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Introduction

Design rationale
How to read this documentation

Test everything that could possibly break --XP maxim

The acceptance test makes the customer satisfied that the software provides the business value that makes them willing to pay for it. The unit test makes the programmer satisfied that the software does what the programmer thinks it does --XP maxim

What is the first thing you need to do when you start working on new library/class/program? That's right - you need to start with the unit test module (hopefully you all gave this answer!). Occasionally, you may get away with simple test implemented using asserts, but any professional developer soon finds this approach lacking. It becomes clear that it's too time-consuming and tedious for simple, but repetitive unit testing tasks and it's too inflexible for most non-trivial ones.

The Boost.Test library provides both an easy to use and flexible set of interfaces for writing test programs, organizing tests into simple test cases and test suites, and controlling their runtime execution. Some of Boost.Test's interfaces are also useful in production (non-test) environments.

Starter example

This is how a minimal single-file test program looks like:

#define BOOST_TEST_MODULE My Test 1
#include <boost/test/included/unit_test.hpp> 2

BOOST_AUTO_TEST_CASE(first_test) 3
{
  int i = 1;
  BOOST_TEST(i); 4
  BOOST_TEST(i == 2); 5
}

1

Macro BOOST_TEST_MODULE defines the name of our program, which will be used in messages.

2

This includes all the Unit Test Framework in a "header-only" mode; it even defines function main, which will call the subsequently defined test cases.

3

Macro BOOST_AUTO_TEST_CASE declares a test case named first_test, which in turn will run the content of first_test inside the controlled testing environment.

4

This test checks if i is non-zero.

5

This test checks if i has value 2 (something more than just evaluating the equality operator).

When run, it produces the following output:

Running 1 test case...
test_file.cpp(8): error: in "first_test": check i == 2 has failed [1 != 2]

*** 1 failure is detected in the test module "My Test"

PrevUpHomeNext