agile3
Engineering Practices

Test Driven Development (TDD)

Writing a failing test first, then just enough code to pass it, then cleaning up: red, green, refactor.

2 min read · Practitioner

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

  1. Red: write a small test for the next bit of behaviour; watch it fail.
  2. Green: write just enough code to pass.
  3. 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.

AI in Testing →

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.