CLI
Pytest · Reference cheat sheet
CLI
Pytest · Reference cheat sheet
📋 Overview
The pytest CLI selects tests, controls verbosity, stops on failure, and passes plugin options. Learn a small set of flags for daily use and CI.
🔧 Core concepts
| Flag | Purpose |
|---|---|
-q / -v | Quiet / verbose |
-x | Stop on first failure |
--lf / --ff | Last failed / failed first |
-k EXPR | Name expression |
-m EXPR | Mark expression |
-s | No capture (show print) |
--pdb | Drop into debugger |
--maxfail=N | Stop after N failures |
-n | xdist workers |
Node id: path/test_file.py::TestClass::test_name.
💡 Examples
Selection:
pytest tests/test_api.py
pytest tests/test_api.py::test_health
pytest -k "user and not slow"
pytest -m "not integration"Debugging:
pytest -x --pdb
pytest --lf
pytest -vv -s
pytest --trace # break at start of each testOutput & reports:
pytest -ra
pytest --tb=short
pytest --junitxml=report.xmlUseful combos:
pytest -q --cov=myapp --cov-report=term-missing
pytest -n auto -m "not slow"
pytest --durations=10Collect only:
pytest --collect-only -q⚠️ Pitfalls
-sflooding CI logs.- Overusing
-kstrings that silently match nothing useful. - Forgetting quotes around mark expressions in shells.
- Running from the wrong cwd so
testpathsmiss files. - Passing pytest args after
--incorrectly in some wrappers.