pytest Cheat Sheet

Notes to self on using pytest.

Options

  • -s — Show content printed to stdout
  • --log-cli-level — Show logging output for specified level
  • --disable-warnings — Suppress warning messages
  • -v — Verbose output
  • -x — Stop after the first failure
  • --maxfail — Stop after specific number of failures
  • --lf / --last-failed — Only rerun the tests that failed previously
  • --ff / --failed-first — Run failed tests first
  • --durations — Show specified number of slowest tests

Run Specific Tests

pytest test_file.py
pytest test_file.py::test_function