Test-driven development embraces three simple rules:
- You are not allowed to write any production code unless it is to make a failing unit test pass.
- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
- You are not allowed to write any more production code than is sufficient to pass the one failing unit test. In short, with Test-Driven Development, you always write production code in response to a failing unit test.
In this tutorial, you will see how to apply the ideas of test-driven development in C++ using the Boost.Test unit testing framework. All the major areas of Boost.Test will be covered from assertions through test case design and organization. We will cover the difference between state-based testing and behavior based testing using mock objects provided through turtle mock, a mock object framework for Boost.Test.
First, you will walk through some exercises designed to show you the mechanics of test-driven development. Next, you will apply the concepts you have learned about test-driven development as a design activity to implement a feature. Once this tutorial is over, you will see how test-driven development is a powerful design activity that will naturally lead you to creating small, cohesive and decoupled classes.