Testing · lesson 4 of 10
Ideas in this lesson
AUnit · integration · system · acceptancethe four levels
BTest pyramidmany fast tests at the bottom, few slow ones at the top
CFakea simple working stand-in for a real service

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:

Pila’s tests at each level
LevelChecksIn PilaSpeed
UnitOne function or class, alonetake_number gives the 61st phone no numberMilliseconds
IntegrationPieces working togethersms.py and the real database save and read a ticketSeconds
System (end-to-end)The whole app, as a user meets itA text arrives, a number is saved, the counter page shows it, the screen updatesMinutes
AcceptanceThat it does what the people paying for it needAte 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.

Step 4 code
Edit the code, then run it again

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.

How could the team make the fake itself catch Jun’s long message?

Spot the trap: only end-to-end tests

Spot the trapKap. Ernesto

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

A test checks that sms.py and the database save and read a ticket together. Which level is it?
Why does the pyramid have many unit tests and few end-to-end tests?
What is a fake?

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