Test-Driven Development: Red, Green, Refactor
Test-Driven Development (TDD) is a software construction technique developed by Kent Beck in the late 1990s as part of Extreme Programming. It guides development through an iterative cycle of writing tests before functional code. The process repeats three core steps:
- Write a test for the next piece of functionality you intend to add.
- Write the functional code to make that test pass.
- Refactor both old and new code to improve its structure.
The Process in Practice
These three steps, frequently summarized as Red - Green - Refactor, form the core of TDD. However, the cycle is preceded by a crucial planning stage where you enumerate a list of test cases. You select one test from this list, apply the red-green-refactor cycle to it, and then proceed to the next test. Selecting the order of tests is a significant skill—the goal is to choose tests that accelerate your progress toward the pivotal points of the design. You should also continuously append any new test ideas to your list as they arise during development.
This approach of writing the test first yields two primary advantages. The first is obvious: it ensures you maintain a self-testing codebase, as every piece of functional code is written specifically to pass a test. The second, subtler benefit is that it forces you to consider the interface of your code before tackling its implementation. This early and deliberate focus on how a class is used helps separate interface from implementation—a fundamental design principle that many developers find challenging.
A Common Pitfall
One of the most frequent mistakes in applying TDD is skipping the refactoring step. Keeping code clean through refactoring is essential; without it, the process degenerates into a chaotic accumulation of code fragments. While the resulting code will still be covered by tests—a less severe outcome than most design failures—it loses a core benefit of the discipline.
Further Reading
For a concise overview from the creator, Kent Beck's canonical description of TDD is an excellent starting point. For more comprehensive material, his book Test-Driven Development is the standard reference. James Shore's chapter in The Art of Agile Development places TDD in a broader agile context, while his Let's Play TDD series offers a practical, walkthrough-based learning path.



