Software engineering / Test-driven development

Test-driven development

Grow a small leap-year function the test-driven way: write a test that fails, write just enough code to pass it, tidy up, and go round again.

Beginner7 steps~6 min
Red — a new test that fails
Green — just enough code to pass (drawn blue)
Refactor — tidy the code, tests stay passing
// THE SHORT VERSION

In 30 seconds

Test-driven development (TDD) means writing the test before the code. First a small test that fails, because the code does not exist yet. Then just enough code to make it pass. Then a tidy-up, with the test guarding against mistakes.

The loop is called red, green, refactor and takes a few minutes each time round.

It pushes you to decide how code will be used before writing it, and leaves a test for every behaviour. It suits well-defined logic better than exploratory work, and not everyone finds it worth the cost.

Test-driven development, step by step

STEPS01 / 07

Red and green are the colours test runners print for fail and pass; the drawings show a pass in blue. Studies of whether TDD improves quality or speed overall have mixed results.

STEP 01 / 07 Test first
THE MATH
test first, then code, then tidy
  1. Step 1: Test first. Usually the code is written first and the tests afterwards, if time allows. Test-driven development (TDD) flips the order: write a small test, then just enough code to satisfy it, then tidy up, and repeat every few minutes.
  2. Step 2: Red: a failing test. We want a function that says whether a year is a leap year. Before writing it, write a test that states what it should answer. Run it: it fails, because the function does not exist. Seeing it fail first shows the test really runs and is able to fail.
  3. Step 3: Green: make it pass. Now write the least code that makes the test pass: a year that divides by 4 is a leap year. 2024 divides by 4 and 2023 does not, so both checks hold. The code is clumsy, and that is fine for now.
  4. Step 4: Refactor: tidy up. With a passing test as a safety net, improve the code without changing what it does. Seven lines shrink to three. Run the test again: still passing, so the tidy-up broke nothing. Changing structure but not behaviour is called refactoring.
  5. Step 5: Repeat for the next rule. Leap years have two more rules: century years such as 1900 are not leap years, unless they divide by 400, like 2000. Each rule arrives the same way: a new test that fails, then one line of code that makes it pass.
  6. Step 6: What it does to design. Writing the test first means using the code before it exists, so you settle its name, inputs and outputs from the caller’s side. Code that is awkward to test tends to be tangled, and you find that out early. A full set of tests comes as a by-product.
  7. Step 7: Criticisms, and where it fits. TDD suits code with clear rules, and bug fixes, where the first test reproduces the bug. It suits exploratory work less: you cannot test first when you do not yet know what to build. Critics add that it slows the start and that tests tied to inner details make later changes harder.
VALUES SO FAR
ordertest, then codeTDD
red····fail
green····pass
refactor····pass
1900····rule 2
2000····rule 3
tests····result

Spotted a mistake on this page?