Test-Driven Development (TDD)

What is Test-Driven Development (TDD)?

Test-driven development (TDD) is a software development practice in which developers write a failing automated test before writing the code to make it pass, then refactor, in short repeated cycles.

Updated November 2026 · 4 min read


Test-driven development (TDD) is a way of writing software in which the test comes before the code. A developer writes a small automated test describing one piece of behavior the code does not have yet, runs it and watches it fail. Then they write just enough code to make it pass, and finally clean up the design while the test keeps them honest. The cycle takes minutes and repeats many times a day, so the code grows in small, verified steps instead of large untested jumps.

Kent Beck, who developed the practice as part of Extreme Programming in the 1990s, described it in his 2002 book Test-Driven Development: By Example. Beck has described it as a rediscovery rather than an invention: he recalled an old programming book advising developers to write out the output they expected before writing the program, and NASA's Project Mercury used test-first development in the early 1960s. What Beck added was a tight, repeatable loop and the argument that tests written first are a design tool, not only a way to catch bugs.

The Red-Green-Refactor Cycle

StepWhat the developer doesWhy it matters
RedWrite one small test for behavior that does not exist yet, and run it to see it failProves the test can fail, and forces a decision about what the code should do before deciding how
GreenWrite the simplest code that makes the test passKeeps each step small and avoids building features nobody asked for
RefactorImprove names, remove duplication and tidy the design, rerunning the tests after each changeKeeps the code clean as it grows, with the tests as a safety net

Robert C. Martin later condensed the discipline into three rules: write no production code except to pass a failing test, write no more of a test than is enough to fail, and write no more production code than is enough to pass. Few teams follow them literally, but they capture the intent: tests and code advance together in very small increments.

What TDD Is Good For

  • Better design. Code that is hard to test is usually too tightly coupled. Writing the test first exposes this immediately and pushes developers toward small, focused units with clear inputs and outputs.
  • A regression suite as a by-product. Every behavior arrives with a test, so the suite grows with the code. This makes later changes and refactoring far safer, and supports continuous integration.
  • Fewer defects reaching testing and production. Problems are found seconds after they are introduced, when they are cheapest to fix.
  • Executable documentation. The tests show, in working code, how each unit is meant to be used.
  • Focus. Working one failing test at a time limits how much a developer has to hold in their head.

What the Evidence Says

Research on TDD is mixed, partly because it is hard to study. The most cited industrial study, by Nachiappan Nagappan and colleagues at Microsoft and IBM, was published in 2008. It found that four teams using TDD had 40% to 90% lower pre-release defect density than similar teams that did not, while taking roughly 15% to 35% longer to write the initial code, a trade-off the teams' managers judged worthwhile. Controlled experiments, often with students, show smaller and less consistent effects. The fairest summary is that TDD tends to improve internal quality and test coverage, its effect on speed depends on how speed is measured, and much of its value comes from the design discipline rather than the tests themselves.

Where TDD Fits and Where It Struggles

TDD works best for business logic, algorithms, data transformations and anything with well-defined inputs and outputs. It is harder to apply to user interfaces, code that wraps external services, exploratory prototypes and legacy code that was never designed for testing. In those cases teams often write tests after the fact, use higher-level tests or first refactor the code so that it can be tested.

TDD operates at the level of individual units. Behavior-driven development and acceptance test-driven development apply the same test-first idea to features and business requirements, and many teams use them together: an acceptance test defines what the feature must do, and TDD drives the code underneath it.

An Example

Consider a hypothetical developer adding a rule to an online store: orders of $100 or more ship free, smaller orders pay $7.95.

  1. Red: they write a test asserting that a $120 order has a shipping cost of $0. It fails because the shipping function does not exist.
  2. Green: they write a function that returns 0. The test passes.
  3. Red: a second test asserts that a $40 order costs $7.95. It fails.
  4. Green: they add a condition: return 0 if the total is at least $100, otherwise 7.95. Both tests pass.
  5. Red: a third test checks the boundary, a $100.00 order, which should ship free. It passes immediately, confirming the condition uses "at least" rather than "more than".
  6. Refactor: they move the $100 threshold and $7.95 fee into named constants and rerun all three tests.

Ten minutes later the rule is implemented, the boundary case is settled, and three tests will catch anyone who breaks it in future.

Common Pitfalls

  • Skipping the refactor step, which leaves a well-tested mess.
  • Testing implementation details instead of behavior, so tests break whenever the code is restructured even though nothing visible changed.
  • Taking steps that are too large, writing a big test and a lot of code at once, which loses the fast feedback that makes TDD work.
  • Treating coverage as the goal. TDD usually produces high code coverage, but chasing the number leads to weak tests.
  • Abandoning it under deadline pressure, which is when the safety net matters most.

TDD is one of the core practices of Extreme Programming, alongside pair programming and continuous integration. Its effect on delivery shows up in measures such as change failure rate in DORA metrics.