In 30 seconds
- Red: write a test that fails.
- Green: write the simplest code to pass it.
- Refactor: improve the code with tests as a safety net.
The cycle
- Red: write a small test for the next bit of behaviour; watch it fail.
- Green: write just enough code to pass.
- Refactor: clean up code and tests.
Kent Beck described TDD in Test-Driven Development: By Example (2002).
Benefits
- Code designed to be testable and modular
- A growing suite of fast unit tests
- Confidence to refactor
- Small steps that keep progress steady
How to start
Pick a small, well-understood piece of logic and practise the cycle. Coding katas help build the habit. Compare with BDD on TDD vs BDD.
Myth vs reality
Myth · TDD doubles development time.
Reality · Upfront time is often repaid by less debugging.
Myth · TDD is about testing.
Reality · It's mainly a design technique.
Myth · You must test every line first.
Reality · Use judgment; some code benefits more than others.
The AI angle
TDD gives AI-generated code a clear target: write the failing test yourself, let AI propose the code, and keep refactoring under your control.
FAQ
Is TDD hard to learn?
The cycle is simple; the skill takes practice.
Does TDD work with legacy code?
Yes, combined with characterization tests.
How small should each step be?
Small enough that each cycle takes minutes.