The Three Rules of TDD

Robert C. Martin (“Uncle Bob”) provides a concise set of rules for practicing TDD.[9]

  1. Write production code only to pass a failing unit test.

  2. Write no more of a unit test than sufficient to fail (compilation failures are failures).

  3. Write no more production code than necessary to pass the one failing unit test.

Rule #1 says to write tests first—understand and specify, in the form of a unit test example, behavior you must build into the system.

Rule #2 says to proceed as incrementally as possible—after each line you write, get feedback (via compilation or test run) if you can before moving on. When test-driving a RetweetCollection class, we stop as soon as we write this much of a first test:

c3/1/RetweetCollectionTest.cpp ...

Get Modern C++ Programming with Test-Driven Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.