Skip to content
GabineteTools for thinking

No. 2045Workflow and delivery

Test-Driven Development

Kent Beck · 2002

Wooden board with row of holes: the full ones show green flags lowered; the last, empty, raises red flag before a newly carved pin.

The table / Model card · No. 2045 · Workflow and delivery

Test-Driven Development

Red, green, refactor: let a failing test define the next bit of code.

Also known as TDD Variation for Biz Processes (Test-Driven Business)

The essentials

What it is

What it solves

How do you write code that does what you intend, stays easy to change, and does not quietly break existing behaviour as the system grows?

When to use it

Use it for business logic, algorithms and libraries with clear inputs and outputs, for code that will be maintained for years, and when changing legacy code that needs a safety net first.

Where it comes from

Kent Beck developed TDD out of the test-first practice of Extreme Programming in the late 1990s, after writing the first xUnit framework in Smalltalk. He says he rediscovered the idea from an older programming book, so test-first coding predates him. His book Test Driven Development: By Example was published by Addison-Wesley in November 2002.

Kent Beck · 2002

How it works

Writing the test first forces a precise statement of the next behaviour before any implementation exists, which clarifies intent and the interface. Seeing the test fail proves the test can detect the missing behaviour. Writing only enough code to pass keeps steps small and avoids speculative design. Refactoring under a full green suite lets design improve safely, because any regression shows up within seconds. Over time the tests become executable documentation and a safety net that makes later changes cheaper. Research suggests the small, steady steps matter as much as the test-first ordering.

You need

A clear next behaviour or requirement, a unit-testing framework, a fast test runner, and the discipline to keep steps small.

You get

Working code, a suite of fast automated tests covering each behaviour, and a design shaped into small, testable units.

Case

Illustrative case

Tollgate, a fictional parking-payments startup, must add a pricing rule: first 15 minutes free, then 2.40 per hour, capped at 18 per day. A developer writes a failing test for 10 minutes costing 0, makes it pass, then adds tests for 75 minutes costing 2.40, 9 hours hitting the 18 cap, and a stay crossing midnight. The midnight test exposes a bug in the day boundary before any customer sees it. The feature ships with 14 tests running in under a second; three months later a cap change from 18 to 20 takes 20 minutes and one test update.

A fictional company used to explain the model.

Caution

When to avoid it

  • It fits poorly with throwaway spikes, exploratory UI layout work, or code dominated by external systems that are hard to isolate.
  • It is not a replacement for integration, performance, security or exploratory testing.

Common mistakes

  • Writing tests after the code and calling it TDD.
  • Taking steps that are too large.
  • Skipping the refactor step.
  • Testing implementation details so tests break on every refactor.
  • Over-using mocks until tests prove nothing about real behaviour.

Known weaknesses

  • Short-term productivity can drop, especially for teams new to it.
  • Tests share the developer's misunderstandings of the requirement.
  • Brittle, over-mocked tests add maintenance cost.
  • Evidence of quality benefit is modest and context-dependent.

How to apply it in 6 steps

How to apply it

Test-Driven Development

6 steps

Use the arrow keys to move between steps, Home and End for the first and last, and Enter to fold or unfold the booklet.

View all 6 steps
  1. List behaviours

    Write down the scenarios the new feature must handle.

  2. Write a failing test

    Pick one scenario and express it as an automated test.

  3. Watch it fail

    Run the suite and confirm the new test fails for the expected reason.

  4. Make it pass

    Write the simplest code that makes the test and all others pass.

  5. Refactor

    Clean up duplication and design while keeping every test green.

  6. Repeat

    Take the next scenario from the list until the feature is complete.

Which decisions it helps with

Decision profile

A phased process to take action.

Keyboard: Tab to the sheet, use the arrows to explore the assessments and their questions, and press Escape to close the note.

IIdentity

Type of tool
Phased process
Decision stage
Plan the work, roles and deadlines
Discipline
Agile and software development
What it produces
A sequence of phases completed in order

IIIResources required

  • TimeOne session (half a day or a full day)
  • PeopleOne person working alone
  • DataNone: experience is enough
  • ExpertiseAnyone who has read a page of instructions
  • Time horizonDays or weeks
  • ScopeOne team
  • FrequencyOccasionally
The team’s own knowledge is enough

IITen axes

The dot shows which end the model favours. A hollow dot means no clear preference.

  • DirectionExecution
  • Top-downBottom-up
  • PeopleProcesses
  • Generate optionsNarrow down options
  • Step-by-step analysisIntuition and experience
  • A strict sequence of stepsA flexible framework
  • Protect what we haveTake a risk
  • Improve what we haveFundamental change
  • Inside the organisationOutside the organisation
  • Understand firstAct now

IVUseful when

Applicable traits

  • How it supports actionDecide what comes first
How was this profile calculated?

An automated system reads each model card and answers the same questions for every model. This profile has answers for 77 of 77. No answers are entered by hand.

A very high probability counts as “yes” and a very low one as “no”. Intermediate answers are hidden.

On the axes, mixed means the model favours neither end.

How it works →

Thinking space

Workflow and delivery

They organize and accelerate day-to-day delivery: Kanban, DevOps, development cycles, automation and work standards.

Membership: 23 % · also “Learning, coaching and goal setting” (21 %)

View in the atlas of spaces →

Each point is a model. Distance reflects answers to the decision questions. Light points share this model’s space.

Similar models

Verified sources

Use the arrow keys to move between sources.

Original work

  1. Beck, K. (2002). Test Driven Development: By Example. Addison-Wesley Professional (Addison-Wesley Signature Series).Defining book for TDD. Publisher page shows title, published Nov 8, 2002 by Addison-Wesley Professional, Signature Series (Beck), ISBN-13 978-0-321-14653-3.View source: Test Driven Development: By Example (opens in a new tab) ↗

Critique

  1. Rafique, Y. and Misic, V. B. (2013). The Effects of Test-Driven Development on External Quality and Productivity: A Meta-Analysis. IEEE Transactions on Software Engineering, 39(6), 835-856.Meta-analysis reporting a small positive effect on quality and little effect on productivity overall. Crossref confirms title, authors, TSE vol. 39, pp. 835-856, 2013.View source: The Effects of Test-Driven Development on External Quality and Productivity: A Meta-Analysis (opens in a new tab) ↗
  2. Fucci, D., Erdogmus, H., Turhan, B., Oivo, M. and Juristo, N. (2017). A Dissection of the Test-Driven Development Process: Does It Really Matter to Test-First or to Test-Last?. IEEE Transactions on Software Engineering.Finds that granularity and uniformity of steps explain outcomes more than test-first ordering. Crossref confirms title, five authors, TSE, July 2017.View source: A Dissection of the Test-Driven Development Process: Does It Really Matter to Test-First or to Test-Last? (opens in a new tab) ↗

Wikipedia article (opens in a new tab) ↗