In a suite of thousands of tests, running exactly the ones you're working on is what keeps the feedback loop fast.
The pytest command line is your daily tool:
- Node ids select precisely:
pytest tests/test_cart.py,pytest tests/test_cart.py::test_total,pytest tests/test_cart.py::TestDiscounts::test_zero_tax. -k "expr"selects by name substring withand,or,not:pytest -k "cart and not slow".-m markerselects by marker (see markers).-xstops at the first failure;--maxfail=3after three.--lf(last failed) reruns only the tests that failed last time;--ffruns them first, then the rest. pytest remembers results in the.pytest_cachedirectory.-vshows each test;-qis quieter;-rchooses the short summary (-ralists everything that didn't pass).-sdisables output capturing soprint()shows up immediately; normally output is shown only for failing tests.--pdbdrops into the debugger on failure;-lshows local variables in tracebacks.--durations=10lists the slowest tests.
pytest --collect-only -q lists what would run without running it, handy to check that discovery and -k expressions do what you expect.
A typical loop while fixing a bug: pytest -x --lf, then the full suite before committing.
Example
# Run one file, one test, one method
pytest tests/test_cart.py
pytest "tests/test_cart.py::test_total_includes_tax"
pytest "tests/test_cart.py::TestDiscounts::test_zero_tax"
# Select by name expression, stop at first failure
pytest -k "cart and not slow" -x
# Rerun only last failures, show prints, debug on failure
pytest --lf -s --pdb
# See what would run, and the 5 slowest tests
pytest --collect-only -q
pytest --durations=5Remember
- Node ids (file::class::test) select exact tests
- -k matches names with and/or/not; -m selects markers
- -x stops early, --lf reruns failures, -s shows print output
- --collect-only -q previews selection
Watch out for
- Debugging with print() and not seeing any output because capture is on (use -s or look at the failure report).