Skip to content

Lesson 9 of 9 · 27m

Designing a Suite People Maintain

Organize unit, integration, and slow tests; choose CI selection deliberately; and keep failure output useful enough that engineers trust the suite.

A test suite is software people operate under pressure. When it is slow, flaky, or impossible to read, engineers route around it. The maintainable suite is not the one with the most tests; it is the one whose failures are actionable and whose cost is proportional to the risk it covers.

Separate test types by cost and confidence

tests/
  unit/test_money.py
  unit/test_slug.py
  integration/test_user_repository.py
  integration/test_stripe_webhook.py
  e2e/test_checkout_flow.py

Unit tests answer quickly whether logic is correct. Integration tests answer whether real collaborators connect. End-to-end tests answer whether a critical user journey still exists. Do not demand that one layer do all three jobs. Mark expensive tests explicitly:

pytestmark = pytest.mark.integration

Configure the marker:

[pytest]
markers =
    integration: tests requiring real external services
    slow: tests too slow for tight local feedback

Then select deliberately:

pytest tests/unit -q
pytest -m integration
pytest -m "not slow"

Optimize the feedback loop, not ideological purity

A pull request needs fast confidence. A nightly job can afford broader exploration. A common split:

  1. Pre-push or pre-commit: targeted tests for changed modules.
  2. Pull request: unit suite plus critical integration tests.
  3. Main branch or nightly: full integration, migration, and end-to-end coverage. Use pytest -n auto with pytest-xdist when tests are isolated enough for parallelism. If parallel execution breaks the suite, that is information: hidden shared state exists. Fix the shared state before celebrating speed.

Failure output is a user interface

Good test names explain behavior:

def test_checkout_rejects_payment_when_inventory_is_reserved():

Bad names such as test_checkout_service_method_3 restate implementation. Inside tests, arrange-act-assert structure matters because failures are read under stress:

def test_checkout_rejects_payment_when_inventory_is_reserved():
    inventory.reserve(sku="SKU-9", quantity=2)
    result = checkout.pay(cart_with("SKU-9", quantity=3))
    assert result.status == "rejected"
    assert result.reason == "insufficient_inventory"

When this fails, the reader knows what invariant broke without reverse-engineering mocks.

Delete tests that only mirror implementation

A test that asserts the exact sequence of private function calls is not protection; it is a refactoring tax. Keep tests that encode externally meaningful behavior, security boundaries, data invariants, and regression lessons. Delete or rewrite tests that fail only because the implementation changed shape while behavior stayed correct.

Field notes

  • Quarantine is not a permanent home for flaky tests; either fix them quickly or delete the false confidence they provide.
  • Put regression tests near the layer where the bug was cheapest to expose.
  • Review test code like production code: names, duplication, fixtures, and assertions all matter.
  • CI should store junit XML (--junitxml=reports/pytest.xml) so flaky and slow-test tooling has structured data.
  • A suite people trust gets run. A suite people fear gets bypassed — trust is the real coverage metric.

Was this lesson helpful?

Sign in to rate