...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
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 assert
s, 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.
This is how a minimal single-file test program looks like:
#defineBOOST_TEST_MODULE
My Test #include <boost/test/included/unit_test.hpp>BOOST_AUTO_TEST_CASE
(first_test) { int i = 1;BOOST_TEST
(i);BOOST_TEST
(i == 2); }
Macro |
|
This includes all the Unit Test Framework in a "header-only"
mode; it even defines function |
|
Macro |
|
This test checks if |
|
This test checks if |
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"