Part 11 / Topic 4 of 12
pytest basics
pytest is the most popular testing tool for Python. It is a third-party package, so install it inside a virtual environment (Part 7). Like an examiner who checks every answer sheet and gives you a report card.
python -m venv .venv # macOS/Linux: python3 -m venv .venv
.venv\Scripts\activate # Windows
source .venv/bin/activate # macOS/Linux
python -m pip install pytest
Naming rules (pytest finds tests by name)
| What | Rule | Example |
|---|---|---|
| Test file | starts with test_ (or ends with _test.py) |
test_shop.py |
| Test function | starts with test_ |
def test_empty_cart_costs_nothing(): |
| Check | plain assert |
assert total == 1500 |
def apply_discount(amount, percent):
return amount * (1 - percent / 100)
def test_ten_percent_off():
assert apply_discount(1000, 10) == 900
def test_no_discount():
assert apply_discount(1000, 0) == 1000
Running
Run from this part's folder:
python -m pytest # find and run all tests
python -m pytest -q tests # -q = quiet: dots and a one-line summary
python -m pytest -v tests # -v = verbose: one line per test
python -m pytest -k discount # -k = only tests whose name contains "discount"
python -m pytest -x # -x = stop at the first failure
python -m pytest tests/test_shop.py::test_empty_cart_costs_nothing # one test
Use python -m pytest (not just pytest): it uses the pytest of the Python you are running, inside your venv.
Reading the results
Each test prints one character: . passed, F failed (an assert was false), E error (the test itself crashed).
If the code had a bug (/ 10 instead of / 100), you would see:
F. [100%]
=================================== FAILURES ===================================
_____________________________ test_ten_percent_off _____________________________
def test_ten_percent_off():
> assert apply_discount(1000, 10) == 900
E assert 0.0 == 900
E + where 0.0 = apply_discount(1000, 10)
test_discount.py:6: AssertionError
=========================== short test summary info ============================
FAILED test_discount.py::test_ten_percent_off - assert 0.0 == 900
1 failed, 1 passed in 0.02s
The > marks the failing line. The E lines show the real value (0.0) next to the expected one (900). The time at the end changes on every run.
How tests/ can import examples/shop.py
tests/ and examples/ are sibling folders, so import shop would normally fail.
pytest automatically loads a file named conftest.py before running the tests in its folder. Ours adds the examples folder to sys.path (the list of folders Python searches when you import, Part 7):
import sys
from pathlib import Path
EXAMPLES_DIR = Path(__file__).parent.parent / "examples"
if str(EXAMPLES_DIR) not in sys.path:
sys.path.insert(0, str(EXAMPLES_DIR))
Because the path is built from the file's own location, the tests work from the part folder (python -m pytest -q tests) and from the repo root (python -m pytest -q stage-2-intermediate/part-11-testing-and-code-quality/tests). See tests/conftest.py.
๐ Going deeper: in a real project you would install your code as a package (
pip install -e ., coming in Stage 3) or setpythonpathin apyproject.toml, instead of editingsys.path.
Read more: the official pytest documentation.
Done here? The practice set for Part 11 โ 7 exercises with solutions โ is on the part page.