Pytest Failure Triage Cheat Sheet

Use this one-page flow to move from a failing test to verified root cause quickly. Full walkthrough: Python Test Failures Triage with Pytest.

1) Immediate Reproduction Commands

GoalCommandNotes
Run single failing testpytest -vv tests/path.py::test_case -x --maxfail=1Keep scope minimal first
Run one parameterized casepytest -vv tests/path.py::test_case[param] -xAvoid mixed signals
Stop on first failurepytest -x --maxfail=1Fast feedback loop
Show local variables in tracebackpytest -vv -lUseful for assertion context
Drop into debugger on failpytest -vv --pdbInspect runtime state immediately

2) Failure Type Decision Table

SignalLikely CauseFirst CommandNext Check
ModuleNotFoundErrorEnv or path mismatchpython --versionpip freeze, sys.path
AssertionErrorBehavior or expected output driftpytest -vv -lCompare outputs in Diff Checker
Intermittent pass/failFlaky state, timing, randomnessfor i in {1..30}; do pytest ...; doneFix seed/time/shared state
Timeout/hangWait condition or deadlockpytest -vv -xAdd strict timeout + logs

3) Environment Sanity Checklist

CheckCommandTarget
Python interpreterwhich python && python --versionMatch CI major/minor version
Pytest/plugin versionspytest --versionNo hidden plugin drift
Dependency lock paritypip freeze | rg -n "pytest|pluggy"Critical package parity
Import path visibilitypython -c "import sys; print('\\n'.join(sys.path))"Expected project path present
Package resolutionpython -c "import pkg; print(pkg.__file__)"Correct module source

4) Flaky Test Isolation Commands

PatternCommandWhy
Repeat test rapidlyfor i in {1..30}; do pytest -q tests/path.py::test_case || break; doneConfirm intermittency
Fix hash randomnessPYTHONHASHSEED=0 pytest -vv tests/path.py::test_caseControl dict/set iteration variance
Order-sensitive suite runpytest -vv tests/path -xExpose state leakage between tests
Capture failing artifactspytest -vv --maxfail=1 > fail.log 2>&1Preserve exact failure evidence

5) Exit Criteria Before Merging

RequirementVerification
Root cause is explicitOne sentence in PR: cause + evidence
Original repro command passesRun same command used in triage start
CI-like scope passesRun relevant directory/module test subset
Prevention guard addedFixture reset, deterministic seed, or stricter assertion