Testing · lesson 6 of 10
Ideas in this lesson
ARedwrite a test that fails
BGreenwrite just enough code to pass it
CRefactortidy up, with the tests still passing

Test-driven development

Last updated

So far the tests came after the code. Test-driven development turns that around: write one small failing test, write just enough code to pass it, tidy up, and repeat. In this lesson you'll build Pila's priority lane for seniors, pregnant women and people with disabilities this way, one test at a time, and weigh what the method is good for and where people disagree about it.

By the end of this lesson you can

  • Follow the red, green, refactor cycle
  • Build a small feature in small steps, one failing test at a time
  • Say what test-driven development is good for, and its limits

Test first

Kent Beck described test-driven development (TDD) in his 2002 book as a short cycle, repeated many times an hour:

The cycle
StepYouThe tests
RedWrite one small test for the next bit of behaviour. Run it and watch it fail.One fails, for the reason you expect
GreenWrite the simplest code that makes it pass. Ugly is fine for now.All pass
RefactorTidy the code and the tests: names, repetition, structure.Still all pass

Watching the test fail first matters: it proves the test can fail (lesson 6.2's trap). Martin Fowler notes that the step people most often skip is the third, and skipping it leaves a pile of working but messy code.

The priority lane

Philippine law gives senior citizens and people with disabilities priority in public services, and the health centre wants Pila to do the same, for pregnant women too. Kap. Ernesto and Ate Cora agreed the rules:

  • Numbers are called in order.
  • A waiting priority number is called before a regular one.
  • But never more than two priority calls in a row, if a regular patient is waiting, so the regular queue keeps moving.

Marco has added priority to take_number, and an empty call_next. The constant PRIORITY_RUN = 2 holds the “two in a row”.

Build call_next, one test at a time

The tests are already written, in the order you'd write them in TDD, so you can work through the cycle. The first passes already. Run, and make only test 2 pass in queue_rules.py, in the simplest way you can. Run again. Then test 3, then test 4. Each time, a new red, then green.

Step 3 code
Edit the code, then run it again

Hints, if you want them. Test 2: the waiting numbers are those in self.by_phone.values() that aren't in self.called; call the smallest and add it to self.called. Test 3: look in the waiting numbers that are in self.priority first. Test 4: count priority calls in a row in self.run, and set it back to 0 after a regular call.

Refactor

Green isn't the end. Read your call_next again as a reviewer would. Common tidy-ups at this point:

  • A name that says what it holds: waiting, priority_waiting, regular_waiting.
  • A small method for a repeated idea, such as _waiting().
  • Comments only where the reason isn't obvious, such as why the run is limited to two.

After each change, run the tests. If they stay green, the change kept the behaviour. That's the safety net TDD gives you: you can make code better without fear, because the tests will tell you at once if you broke it.

What it’s good for, and the debate

TDD's supporters say it gives small, safe steps; code that is easy to test because it was tested from the first line; and a full set of tests as a side effect. Steve Freeman and Nat Pryce's book Growing Object-Oriented Software, Guided by Tests shows it used to grow a whole system.

It also has thoughtful critics. In 2014 David Heinemeier Hansson, who created the Ruby on Rails framework, argued that test-first had become a dogma, and that bending a design so every piece could be unit-tested made some code worse. He, Kent Beck and Martin Fowler then discussed it in public in a series of conversations called “Is TDD Dead?”. Where they ended up: TDD suits some people and problems better than others, and what matters is having tests you trust, however they were written.

Pila's team uses TDD for rules like the priority lane, where the behaviour is clear and getting it wrong hurts, and writes tests after for exploratory work like trying a new screen layout.

Spot the trap: five tests, one giant change

Spot the trapJun

Jun is trying TDD for the cancel feature. He writes five tests at once, all red, then spends the afternoon on one big change. At 5 p.m. three pass and two fail, and he can’t tell which part of his change is wrong.

What happens if…

Check what you learned

In TDD, what comes right after writing a failing test?
Why watch a new test fail before making it pass?
What did the “Is TDD Dead?” conversations conclude?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development