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 astest_queue.py. - Each test is a function whose name starts with
test_. - Inside, a plain
assertstates 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.
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:
| Part | Does | In test_same_phone_gets_same_number |
|---|---|---|
| Arrange | Set up the situation | A new queue, and the phone already has a number |
| Act | Do the one thing being tested | The same phone texts again |
| Assert | Check the result | It 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).
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
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
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.
- pytest: Get started: test files, test functions, and running them
- pytest: How to write and report assertions in tests: plain assert, and the detailed failure report
- pytest: Good integration practices: where tests live and how pytest finds them
- Martin Fowler: Given When Then: the same three-part shape as arrange, act, assert