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
pytest -k "user and not slow"

# Filter tests by substring expression.

Run tests by marker
pytest -m integration

# Target a marked test group.

Stop after N failures
pytest --maxfail=2

# Limit noise during triage.

## Fixtures and Output
Show fixture setup order
pytest --setup-show

# Trace fixture behavior during a run.

Show fixture setup plan
pytest --setup-plan

# Print fixture plan without full execution noise.

Disable all capture
pytest --capture=no

# See stdout, stderr, and interactive output directly.

## CI and Flaky Tests
Re-run a failing test repeatedly
while pytest tests/test_jobs.py::test_sync_worker; do :; done

# Loop a single test to reproduce flakiness locally.

Print key environment values in a test
import os

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

# Reveal CI-only configuration mismatches.

Recommended next

No recommendations yet.