Test levels and the pyramid
Last updated
A unit test checks take_number alone. But Pila is also texts arriving from a gateway, a database, a page at the counter, and a screen in the waiting room. Something has to check that the pieces work together, and that the whole thing does what the health centre needs. In this lesson you'll meet the four levels of testing, see why teams want many small tests and few big ones, and test Pila's SMS code with a fake gateway, so no real text is sent.
By the end of this lesson you can
- Name the four test levels and what each one checks
- Explain the test pyramid and why fast tests should be the many
- Use a fake in place of a real service in a test
Four levels
Lesson 2.3's V-model paired each kind of test with the document it checks. The same four test levels are used by almost every team, whatever their life cycle:
| Level | Checks | In Pila | Speed |
|---|---|---|---|
| Unit | One function or class, alone | take_number gives the 61st phone no number | Milliseconds |
| Integration | Pieces working together | sms.py and the real database save and read a ticket | Seconds |
| System (end-to-end) | The whole app, as a user meets it | A text arrives, a number is saved, the counter page shows it, the screen updates | Minutes |
| Acceptance | That it does what the people paying for it need | Ate Cora runs a morning’s queue on a test copy of Pila (lesson 6.5 automates some of this) | Hours, with people |
The lower the level, the faster, cheaper and more exact the test: a failing unit test points at one function. The higher the level, the more real the test: only a system test shows that the pieces really fit.
The pyramid
In 2009 Mike Cohn drew the test automation pyramid: lots of unit tests at the bottom, fewer service (integration) tests in the middle, and only a few tests through the user interface at the top. Ham Vocke's 2018 article on Martin Fowler's site sums up how teams use it today: write tests at different levels, and have fewer tests the higher up you go.
The reason is time. Pila's 48 unit tests run in two seconds, so Marco runs them every few minutes. If every check were an end-to-end test, the suite would take an hour, nobody would run it before pushing, and a bug would be found a day after it was written instead of a minute.
Teams have proposed other shapes since, such as Kent C. Dodds's “testing trophy”, which puts more weight on integration tests. They agree on the main point: most checks should be fast enough to run all the time, and the slow ones should check what only they can.
Test doubles
sms.py sends replies through an SMS gateway, a paid service. A test that used the real gateway would send real texts, cost money, need the internet, and be slow. So the test hands handle a test double: an object that stands in for the real one, like a stunt double in a film. Martin Fowler lists the kinds:
- A fake works, but in a simpler way: a gateway that keeps messages in a list instead of sending them.
- A stub gives back canned answers.
- A mock is set up to expect certain calls, and fails the test if they don't happen.
Pila's handle takes the gateway as an argument, instead of creating it inside. That small design choice is what makes it easy to pass a fake.
Test sms.py with a fake gateway
The first two tests pass. The third fails on purpose: replace [] with what the fake should have received when someone texts at 17:30. The reply text is in sms.py, in the next tabs.
When the fake lies
A fake is only as good as its likeness to the real thing. Pila's fake accepts any message. The real gateway refuses messages over 160 characters. One day Jun makes the sorry reply friendlier and longer: every unit test passes, and at the health centre, patients who text too late get no reply at all.
That's what integration tests are for. Pila keeps a few tests that talk to the gateway's test mode, which checks messages like the real service but doesn't deliver them, and runs them before each release. The unit tests stay fast; the integration tests check the fake is still telling the truth.
Spot the trap: only end-to-end tests
Kap. Ernesto has heard that end-to-end tests are the most realistic. “Why not just test everything that way?” he asks.
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.
- Mike Cohn: The Forgotten Layer of the Test Automation Pyramid: the pyramid, from the person who drew it
- Ham Vocke: The Practical Test Pyramid (martinfowler.com, 2018): how teams use the pyramid today, with test doubles
- Martin Fowler: Test Double: dummies, fakes, stubs, spies and mocks
- ISTQB Glossary: test level: unit, integration, system and acceptance, defined