Testing · lesson 2 of 10
Ideas in this lesson
Adef test_…():a test is a function whose name starts with test_
Bassertfails the test if the condition is false
Cpython -m pytestfinds the tests, runs them, reports

Unit tests with pytest

Last updated

In lesson 5.P you checked take_number by printing results and reading them. That works once. A unit test does the same check as code, so it can run again after every change, in a second, and say exactly what broke. In this lesson you'll write tests for Pila's queue rules with pytest, read a failing test's report, and use a test to find a real bug.

By the end of this lesson you can

  • Write unit tests as functions with assert, and run them with pytest
  • Structure a test as arrange, act, assert
  • Read a pytest failure report and find the bug it points at

A test is code that checks code

A unit test checks one small piece, a unit, usually a function or a method, in isolation: given this, it should do that. With pytest, the testing tool most Python projects use:

  • Tests live in files named test_*.py, such as test_queue.py.
  • Each test is a function whose name starts with test_.
  • Inside, a plain assert states what must be true. If it's false, the test fails.

Running python -m pytest finds every test, runs each one, and reports. Nothing to read by eye: a dot for each pass, an F for each failure, and a summary.

Run Pila’s first tests

Here are three tests for take_number, with queue_rules.py from lesson 5.P in the next tab. Press Run: pytest runs in this page, the same way it runs on a computer.

Step 2 code
Edit the code, then run it again

On your own computer, with pytest installed in the project's virtual environment (python -m pip install pytest), you'd type python -m pytest in the project folder and see the same thing.

Arrange, act, assert

Most tests have three parts, often called arrange, act, assert:

The shape of a test
PartDoesIn test_same_phone_gets_same_number
ArrangeSet up the situationA new queue, and the phone already has a number
ActDo the one thing being testedThe same phone texts again
AssertCheck the resultIt got the same number back

It's the same shape as lesson 3.4's acceptance criteria: given, when, then. A test's name says what it checks, so a failure report reads like a sentence: test_same_phone_gets_same_number FAILED.

Red, then green

Jun has changed the limit check in queue_rules.py in this version. The fourth test, for the 61st phone, is new. Run it: it fails (testers call this red). Read pytest's report: it shows the failing assert, and the value it actually got. Find the bug in the queue_rules.py tab, fix it, and run again until all four pass (green).

Step 4 code
Edit the code, then run it again

The test found a bug a quick try would miss: you'd have to send 61 texts by hand. Now it's checked in a fraction of a second, every time anyone changes the file.

Spot the trap: the test that can’t fail

Spot the trapMarco

Marco adds a test for the opening hours: it calls q.take_number("0917 111 1111", time(6, 0)) and stops there. pytest says it passes, and the pull request shows one more test.

What happens if…

Check what you learned

How does pytest know which functions are tests?
In arrange, act, assert, what goes in “act”?
A test passes even when you break the code on purpose. What does that tell you?

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