Code Reference

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

FlagPurpose
-q / -vQuiet / verbose
-xStop on first failure
--lf / --ffLast failed / failed first
-k EXPRName expression
-m EXPRMark expression
-sNo capture (show print)
--pdbDrop into debugger
--maxfail=NStop after N failures
-nxdist 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 test

Output & reports:

pytest -ra
pytest --tb=short
pytest --junitxml=report.xml

Useful combos:

pytest -q --cov=myapp --cov-report=term-missing
pytest -n auto -m "not slow"
pytest --durations=10

Collect only:

pytest --collect-only -q

⚠️ Pitfalls

  • -s flooding CI logs.
  • Overusing -k strings that silently match nothing useful.
  • Forgetting quotes around mark expressions in shells.
  • Running from the wrong cwd so testpaths miss files.
  • Passing pytest args after -- incorrectly in some wrappers.

On this page