Python Test Debugging Cheat Sheet

pytest and unittest debugging patterns for isolating failures, flaky tests, fixtures, and CI-only issues.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Selecting What to Run

Run tests matching a keyword

Filter tests by substring expression.

bashANYpytestselection
bash
pytest -k "user and not slow"

Run tests by marker

Target a marked test group.

bashANYpytestmarkers
bash
pytest -m integration

Stop after N failures

Limit noise during triage.

bashANYpytestfail-fast
bash
pytest --maxfail=2

Fixtures and Output

Show fixture setup order

Trace fixture behavior during a run.

bashANYpytestfixtures
bash
pytest --setup-show

Show fixture setup plan

Print fixture plan without full execution noise.

bashANYpytestfixtures
bash
pytest --setup-plan

Disable all capture

See stdout, stderr, and interactive output directly.

bashANYpytestcapture
bash
pytest --capture=no

CI and Flaky Tests

Re-run a failing test repeatedly

Loop a single test to reproduce flakiness locally.

bashANYpytestflaky
bash
while pytest tests/test_jobs.py::test_sync_worker; do :; done

Print key environment values in a test

Reveal CI-only configuration mismatches.

pythonANYpytestenvironmentci
python
import os

def test_env_debug():
    print({k: os.environ.get(k) for k in ['CI', 'APP_ENV', 'DATABASE_URL']})

Recommended next

No recommendations yet.