Skip to content

Lesson 1 of 9 · 20m

The pytest Execution Model

How pytest collects, injects, and reports tests — and why understanding that model prevents most mysteriously skipped or duplicated tests.

The first pytest suite I inherited had 900 tests and nobody trusted it. Tests silently did not run because files were named wrong, fixtures were shadowed three directories deep, and half the failures disappeared when run one at a time. The fix was not more tests. It was learning pytest's execution model.

Collection is a separate phase

pytest first collects tests, then runs them. Collection follows naming rules:

tests/
  test_invoice.py        # collected: files start with test_ or end with _test.py
  invoice_helpers.py     # not collected as tests

Inside those files, pytest collects functions named test_* and methods in classes named Test* that do not define __init__. If a test is "not running," check collection before touching logic:

pytest --collect-only -q

This prints exactly what pytest sees. It is the fastest way to spot a typo like def testinvoice_total() or a class constructor accidentally disabling collection.

Tests are plain function calls with injected arguments

pytest does not require a base class or framework-specific assertions. A test is a function whose parameters are resolved by fixture name:

def test_total_includes_tax(cart, tax_rate):
    assert cart.total(tax_rate) == 11.0

At runtime pytest inspects the signature, finds fixtures named cart and tax_rate, creates them, and calls the test. This means names are load-bearing. Renaming a parameter without renaming the fixture produces fixture 'cart' not found at setup time.

Assertion rewriting is why plain assert is enough

pytest rewrites assert statements at import time so failures show useful diffs:

def test_user_payload():
    assert build_payload(user) == {"id": user.id, "role": "member"}

Use plain assert in almost all tests. Avoid giant custom assertion helpers that hide values; pytest's output is often better when comparison happens directly in the test.

A failing test has lifecycle stages

Every test reports setup, call, and teardown separately. A failure during setup is not the same as a failing assertion. Read the report sections carefully: ERROR at setup means the system under test may never have executed. This matters when diagnosing flaky database state or leaking resources.

Field notes

  • Run pytest -x while repairing a broken suite; stop at the first failure instead of being buried by downstream noise.
  • Use pytest path/to/test_file.py::test_name to run one test exactly; guessing shell quoting costs time.
  • -q reduces noise locally, but keep default verbosity in CI until the suite is stable.
  • Put tests in a top-level tests/ directory unless the project already has a strong convention; consistency beats cleverness.
  • Treat collection errors as build failures, not warnings — a suite that cannot import cannot protect anything.

Was this lesson helpful?

Sign in to rate