Skip to main content

Test-driven development (TDD)

Overview

TDD cycles red → green → refactor: write a failing test that expresses desired behavior, implement the minimum code to pass, then refactor while keeping tests green. It is both a design discipline and a safety net, especially valuable in complex domains with evolving rules.

Key concepts

  • Outside-in vs inside-out — Start from acceptance tests or from small units.
  • Baby steps — Smallest increment that moves the needle.
  • Refactor with confidence — Tests lock behavior while structure changes.
  • Not a substitute for other tests — Still need integration/E2E where appropriate.
  • Learning curve — Upfront slowdown until fluency; pair/mob helps.

TDD micro-loop

Sample: order of steps (pseudocode)

  1. Test: calculateDiscount(100, 'SAVE10') returns 90fails (not implemented).
  2. Implement: return price * 0.9 for known code — passes.
  3. Refactor: extract table of promo rules; add second case with tests.

References