Skip to main content

Your First pytest Tests

Why This Matters

pytest is the standard test tool for Python. Its tests are plain functions with plain assert statements, which makes them easy to read for people and for AI assistants. Knowing four of its features is enough for everything in this course.

The pytest documentation is short and good:

The pytest documentation page Get Started, showing how to install pytest into a virtual environment and create your first test

Screenshot of docs.pytest.org.

1. A test is a function that starts with test_

def test_a_correct_order_has_no_issues(catalog, today):
    assert validate(make_order(), catalog, today) == []

pytest finds files named test_*.py and runs every function whose name starts with test_. Name tests after the behaviour they protect, so a failure tells you what broke without reading the code.

2. assert shows you the difference

If an assert fails, pytest shows both sides: what you got and what you expected. You will see that in lesson 8, Testing Business Rules: Boundaries and Breaking It on Purpose.

3. Fixtures provide what a test needs

tests/conftest.py defines fixtures that any test can ask for by naming them as a parameter:

@pytest.fixture
def today():
    # A fixed "today" keeps date rules reproducible: no test that passes in November and fails in January.
    return date(2026, 12, 1)

catalog, today and erp are fixtures. A fixture can also clean up after the test, as the fake ERP server does in lesson 10, Testing Integrations: Contract Tests and Shared State.

4. Parametrise to test many inputs with one test

@pytest.mark.parametrize(
    "answer",
    [
        "Sure! Here is the order: {...}",  # chatty answer instead of JSON
        '{"supplier": "x"}',  # fields missing
        '{"supplier": "x", "order_number": "PO-1", "delivery_date": null, "lines": []}',  # no date
    ],
)
def test_an_unusable_answer_raises_extraction_error(answer):
    with pytest.raises(ExtractionError):
        extract_order("any message", RecordedLLM(answer))

One test function, three test cases, each reported separately. pytest.raises checks that an error is raised.

Commands you will use

python -m pytest                              # all tests
python -m pytest -vv                          # every test by name
python -m pytest tests/test_validate.py       # one file
python -m pytest -k price                     # tests with "price" in the name
python -m pytest -x                           # stop at the first failure

Exercise

  1. Run python -m pytest -vv and read the 19 test names. Could you explain what the bot does from the names alone?
  2. Find a test that uses the erp fixture and one that uses pytest.raises.
  3. Ask an AI assistant to write a test that an order for MAT-AIR at 79.00 has no issues. Check: does it use the existing make_order helper and fixtures, and is the expected value yours?

Key Takeaways

  • Tests are functions named test_... with plain assert statements.
  • Name tests after behaviour.
  • Fixtures provide shared inputs, such as a fixed today.
  • Parametrise to cover many inputs with one test.