pytest in the real world
Most Python teams use pytest. It finds test_* functions in test_*.py
files, and a plain assert gives a detailed report when it fails. pytest is not
available in the browser, so here is what it looks like:
import pytest
@pytest.mark.parametrize("text, want", [
("Hello World", "hello-world"),
("", ""),
(" a ", "a"),
])
def test_slugify(text, want):
assert slugify(text) == want
@pytest.fixture
def cart():
return Cart(items=["apple"])
def test_total(cart): # pytest passes the fixture in by name
assert cart.total() == 1
parametrize runs one test once per row, so adding a case is one line.
A fixture is a function that builds something a test needs. pytest calls it
and passes the result in by argument name, fresh for each test.
You run it with pytest from the shell. The program shows a hand-rolled
version of both ideas. Run it, then add a row to cases.