Running and selecting tests

pytest · Essentials · 8 min

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 with and, or, not: pytest -k "cart and not slow".
  • -m marker selects by marker (see markers).
  • -x stops at the first failure; --maxfail=3 after three.
  • --lf (last failed) reruns only the tests that failed last time; --ff runs them first, then the rest. pytest remembers results in the .pytest_cache directory.
  • -v shows each test; -q is quieter; -r chooses the short summary (-ra lists everything that didn't pass).
  • -s disables output capturing so print() shows up immediately; normally output is shown only for failing tests.
  • --pdb drops into the debugger on failure; -l shows local variables in tracebacks.
  • --durations=10 lists 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=5

Remember

  • 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).

Check yourself

  1. You fixed two failing tests out of 800. What reruns just those two?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.pytest.org/en/stable/how-to/usage.html