Software engineering · 5 min
Test-driven development
Learn about writing tests first.
What the sources describe
Test-driven development, or TDD, is a way of writing software in which programmers write a test before writing the code. Kent Beck promoted it in the late 1990s as part of Extreme Programming, and described it in a 2002 book. He said he had rediscovered an older idea.
TDD follows a cycle often called red, green, refactor. First, write a test for a small feature; it fails because the feature does not exist yet, shown as red. Then write just enough code to pass the test, shown as green. Finally, refactor, improving the code's design while keeping tests passing.
Supporters say TDD encourages clear, small designs and creates a safety net of tests for future changes. A 2008 study of teams at IBM and Microsoft found TDD teams had far fewer defects, about 40 to 90 percent fewer, but took about 15 to 35 percent longer initially.
Other studies found smaller or mixed effects, and results depend on the team and task. Many developers use parts of TDD or write tests alongside code rather than strictly first.
Connecting the ideas
Test-driven development, promoted by Kent Beck, means writing a failing test first, then code to pass it and then improving the design; a 2008 study found far fewer defects but more initial time.
The big idea
Deciding what success looks like first helps you build the right thing.
Use it in real life
Before starting a task, such as writing a recipe, packing for a trip or completing homework, write a short checklist of how you will know it is done correctly. Complete the task, then check against your list. Note whether defining success first changed how you worked.
Try it
Made-up example: What is the red, green, refactor cycle?
A good answer: write a failing test, write code to pass it, then improve the code while tests keep passing.
Quick quiz
1. Why does a new test fail at first in TDD?
2. What did the 2008 IBM and Microsoft study find?
3. Why do results on TDD vary?
Finished reading?
Make this the next Doom video?
Vote for the lessons you'd most like to see turned into a short animated story.
How was this lesson?
No ratings yet — be the first.
Sign in to rateSources
Overview sources only. These references are not a completed primary-source fact-check.
Limitations
Mixed evidence.
Voice: Debby — Soft and soothing (tap to change)
Debby narration · part 1 of 15
