Testing with pytest: run, read and write Python tests
How to run pytest, read a failing test, select tests with markers and -k, share setup with fixtures, cover many cases with parametrize, and test exceptions.
Tests are how you know your code works, and how you'll know it still works after you change it. In the Devventura Python projects, tests come with the starter: they describe what the ticket asks for, and your job is to make them pass. This guide shows how to use them well, and how to write your own.
Running tests
From the project folder, with your virtual environment active:
python -m pytest # every test
python -m pytest -m m1 # only milestone 1 (tests marked m1)
python -m pytest -x # stop at the first failure
python -m pytest -k customer # tests whose name contains "customer"
python -m pytest tests/test_m1_shipment_model.py::test_rejects_bad_customer_idsUse python -m pytest rather than pytest: it guarantees the pytest of your virtual environment
runs, with your project folder on the import path.
Reading a failing test
A failure looks like this:
def test_base_price_depends_on_the_recipient_zone(data, country, cents):
> assert total(data, country) == cents
E assert 890 == 1190
E + where 890 = total({...}, 'PL')
tests/test_m2_delivery_quotes.py:43: AssertionError
FAILED tests/test_m2_delivery_quotes.py::test_base_price_depends_on_the_recipient_zone[PL-1190]Read it from the bottom up:
- Which test failed, and with which parameters:
[PL-1190]. - What was compared: your code returned 890, the test expected 1190.
- Where: the line with
>.
Then open the test and read it. A good test name tells you the rule ("base price depends on the recipient zone"), and the numbers tell you the case. Here, Poland got the zone 2 price: probably a typo in the zone table.
A NotImplementedError: ... is not implemented yet means you haven't started that function yet.
That's the starter's way of failing clearly.
Writing a test
A test is a function whose name starts with test_, in a file whose name starts with test_. It
arranges some input, acts by calling your code, and asserts what should be true:
def test_express_adds_half_of_the_subtotal() -> None:
shipment = make_shipment(service="express") # arrange
result = quote(shipment) # act
assert result.total == 490 + 245 # assertPlain assert is all you need: pytest rewrites it to show both sides when it fails.
Testing that something fails
import pytest
def test_rejects_unknown_countries() -> None:
with pytest.raises(ValidationError) as caught:
create_shipment(bad_data, shipment_id="X", now=NOW)
assert caught.value.fields == {"recipient.country"}The test fails if no exception, or a different one, is raised. caught.value is the exception,
so you can check what it says.
Many cases: parametrize
When the same rule has many examples, list them instead of copying the test:
@pytest.mark.parametrize(
("grams", "cents"),
[
(5000, 490), # the first 5 kg are included
(5001, 550), # a started kilogram
(6001, 610),
],
)
def test_charges_every_started_kilogram(grams: int, cents: int) -> None:
assert price_for(grams) == centsEach row runs as its own test, with its own pass or fail. Boundaries (5000 vs 5001) are where bugs hide, so test both sides.
Shared setup: fixtures
A fixture prepares something tests need. A test asks for it by naming it as a parameter:
# tests/conftest.py: fixtures here are available to every test file
@pytest.fixture
def data() -> dict[str, Any]:
return copy.deepcopy(VALID) # a fresh copy for every test
def test_requires_a_city(data: dict[str, Any]) -> None:
del data["recipient"]["city"] # safe: no other test sees this change
...The deepcopy matters: if every test shared one dict, a test that deletes a field would break the
tests that run after it.
Markers: labelling tests
Markers are labels. The Devventura projects label each test file with its milestone:
pytestmark = pytest.mark.m1 # every test in this file is an m1 testpython -m pytest -m m1 then runs exactly that milestone. The markers are declared in
pyproject.toml; --strict-markers turns a typo like m11 into an error.
Writing your own tests
The stretch tickets come without tests: writing them is part of the job. A good habit is to turn each acceptance criterion into at least one test before you write the code. Start with the happy path, then the boundaries, then the errors. When a bug is reported, write a test that reproduces it first, then fix it: the test makes sure it never comes back.
Questions and answers
- How do I run only some pytest tests?
- Use -m to select by marker (python -m pytest -m m1), -k to select by name (-k customer), or give a file or test path (python -m pytest tests/test_m1.py::test_name). Add -x to stop at the first failure.
- What is a pytest fixture?
- A function decorated with @pytest.fixture that prepares something a test needs, like a fresh copy of test data. A test asks for it by naming it as a parameter, and pytest calls the fixture for every test that uses it.
Read next
- Python dataclasses, enums and datetimes: modelling a business domain
Model real business data in Python with dataclasses, frozen value objects, StrEnum for fixed choices, type hints checked by mypy, and timezone-aware datetimes.
- How to work with tickets and acceptance criteria as a junior developer
How to read a ticket, find what it doesn't say, ask good questions, and turn acceptance criteria into tests, with examples from realistic product tickets.
- Code review checklist for junior developers
What senior engineers look for in a code review, as a practical checklist: correctness, edge cases, tests, naming, structure and performance, with C++-specific points.