Choosing test cases
Last updated
take_number can be called at any minute of the day, by any phone, with any number of others already in the queue. You can't test them all, so which few do you pick? In this lesson you'll split inputs into groups that should behave the same, test the edges between the groups where bugs gather, and write many cases at once with pytest's parametrize.
By the end of this lesson you can
- Split an input into equivalence partitions and pick one value from each
- Find the boundaries between partitions and test both sides of each
- Write many cases as one parametrised test in pytest
You can’t test everything
take_number takes a time of day. Counting just minutes, that's 1,440 possible times; add how many numbers are already taken (0 to 60 and beyond) and whether the phone has texted before, and there are hundreds of thousands of combinations. Real programs have far more.
So testers choose a few cases that are most likely to show a bug if there is one. Two techniques, used together, do most of the work: equivalence partitioning and boundary value analysis.
Equivalence partitions
An equivalence partition is a group of inputs the code should treat the same way. If one value from the group works, the others very likely do too, so one test per partition is enough to start.
| Input | Partitions | One value from each |
|---|---|---|
| Time of the text | Before 7:00 (no number) · 7:00 to 16:59 (a number) · 17:00 and later (no number) | 5:00, 10:00, 20:00 |
| Numbers already given today | 0 to 59 (a number) · 60 or more (no number) | 10, 60 |
| Has this phone texted today? | No (a new number) · Yes (the same number again) | a new phone, a repeat |
Partitions come from the requirements, not the code: “7:00 until the centre closes at 17:00” is in the acceptance criteria from lesson 3.P. That way the tests check what Pila should do.
Boundaries
Bugs gather at the edges between partitions. Programmers write < when they meant <=, or count from 0 when they meant 1: these are called off-by-one errors. Boundary value analysis tests the values on each side of every edge.
For opening hours, the edges are at 7:00 and 17:00, so the boundary values are 6:59 and 7:00, and 16:59 and 17:00. For the daily limit, they're the 60th and the 61st phone.
0 of 6 placed right
The 60th phone of the day (we already test the 1st)
The 61st phone of the day
The 30th phone of the day (we already test the 2nd)
A text at 14:00 (we already test 10:00)
A text at 16:59
A text at 3:00 (we already test 6:59)
Find the closing-time bug
This test runs once for each case in its list: @pytest.mark.parametrize hands each pair to the test as at and gets_number. The two cases inside opening hours pass. Add the four boundary cases, run it, and read which one fails. Then fix queue_rules.py in the next tab so all six pass.
Each case appears in pytest's report with its values, such as test_opening_hours[at5-False], so a failure says which case broke.
The case nobody tried
Partitions also include inputs that are valid but rare. The failure of the UK's air traffic control flight planning system in August 2023 started with one of them.
- 2018 A new system
Nothing was wrong with the flight plan: it was an unusual member of the partition “valid flight plans”. Pila has its own rare-but-valid inputs: two patients sharing one phone, a text sent at exactly 7:00:00, a phone number with spaces in a different place. Asking “what's valid but unusual?” is part of choosing test cases.
Spot the trap: 10:00 and 11:00
Marco’s pull request tests opening hours at 10:00 and 11:00. Both pass. “Covered,” he writes.
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.
- ISTQB Glossary: equivalence partitioning: the technique, defined
- ISTQB Glossary: boundary value analysis: testing at the edges of partitions
- pytest: How to parametrize fixtures and test functions: @pytest.mark.parametrize
- UK CAA, CAP2993: Independent Review of NATS (En Route) Plc’s Flight Planning System Failure on 28 August 2023, Final Report: the flight plan the system had never met