The Way of TDD
Test-Driven Development (TDD) is the practice of working in a structured cycle where writing tests comes before writing production code. The process involves three steps, sometimes called the red-green-refactor cycle:
- Write a failing test
- Make the test pass by writing just enough production code
- Refactor the production code to meet your quality standards
Research shows TDD has several benefits: it improves test coverage, reduces the number of bugs, increases confidence, and facilitates code reuse. This practice also helps reduce distractions and keep you in the flow. TDD also has its limitations and is not a silver bullet! See the Wikipedia article about TDD for a detailed explanation and references.
Here is a short practical example. Assume you need to modify the following voting algorithm to support the option for voters to abstain:
1. We start by writing a failing test - as expected, the test doesn't even compile:
2. We fix the compilation error by including the missing enum option:
Now that the test compiles, we fix the production code to get all tests passing:
3. We now refactor the code to improve clarity, and complete an iteration of the TDD cycle:
