Testing from acceptance criteria
Last updated
In lesson 3.P the team wrote acceptance criteria for Pila's first story with Kap. Ernesto: how the people paying for Pila will know it's done. Those sentences are already test cases, waiting to be typed. In this lesson you'll turn them into pytest tests, see why tests should check what the software does rather than how it does it, and meet behaviour-driven development, which builds a whole way of working on this idea.
By the end of this lesson you can
- Turn Given–When–Then acceptance criteria into arrange–act–assert tests
- Write tests that check behaviour, not how the code is built
- Explain how tests act as documentation that can’t go out of date
Criteria are tests in waiting
Lesson 3.4 wrote acceptance criteria as Given–When–Then: given a situation, when something happens, then this should result. Lesson 6.2 wrote tests as arrange–act–assert. They're the same three parts with different names:
| Acceptance criterion | Test | Pila’s “one number per phone” |
|---|---|---|
| Given | Arrange | A phone already has number 1 today |
| When | Act | The same phone texts again |
| Then | Assert | It gets number 1 again |
So the criteria Kap. Ernesto agreed to can become tests almost word for word, and a test's name can say the criterion in plain words.
Write the three tests
Each test has its criterion as comments, and the “Given” already done. Replace each assert False line with the “When” (a call to take_number) and the “Then” (an assert). Run until all three pass.
These tests are a kind of documentation that can't quietly go out of date: if Pila stops doing what a test name says, the test fails. A new teammate can read the test names to learn the rules.
What, not how
A good test checks what the software does, as someone using it would see it, and not the details of how it's built inside. Tests that know about the inside break whenever the code is tidied, though nothing a user sees has changed. Then people stop trusting them.
0 of 5 placed right
assert q.take_number(phone, time(9, 0)) == firstassert phone in q.by_phoneassert gateway.sent[0][1].startswith("You are number")assert len(q.by_phone) == 60assert q.take_number(phone, time(17, 0)) is None
Behaviour-driven development
In the early 2000s, Dan North found that new programmers learning test-driven development (lesson 6.6) kept asking what to test, and what to call their tests. His answer was to talk about behaviour: name each test as a sentence about what the software should do, and write the scenarios with the people who want the software, in Given–When–Then. He called it behaviour-driven development (BDD).
Some teams use tools that run plain-language scenarios directly as tests, so the business can read and even write them. Pila's team keeps it simpler: criteria in Given–When–Then, agreed with Kap. Ernesto, and pytest tests named after them. The idea that matters is shared: the people who need the software and the people who build it agree on examples before the code is written.
Spot the trap: the test that knows too much
Jun tests “one number per phone” with: take a number, then assert len(q.by_phone) == 1. It passes. Next month Marco changes Queue to keep tickets in a list, for the waiting-room screen.
What happens if…
Check what you learned
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.
- Martin Fowler: Given When Then: the style, and its link to arrange, act, assert
- Dan North: Introducing BDD: where behaviour-driven development began
- Ham Vocke: The Practical Test Pyramid (martinfowler.com, 2018): testing observable behaviour, not implementation details
- pytest: How to write and report assertions in tests: plain assert