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)
- Test:
calculateDiscount(100, 'SAVE10')returns90— fails (not implemented). - Implement: return
price * 0.9for known code — passes. - Refactor: extract table of promo rules; add second case with tests.