Cribsheet — Pytest

Notes to self on using pytest.

Run Specific Tests

pytest test_file.py
pytest test_file.py::test_function

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

Plugins

pytest-cov

  • --cov= — Path of folder. Set to . for current folder.
  • --cov-report=term — Report in terminal.
  • --cov-report=term-missing — Report in terminal with lines not covered.
  • --cov-report=html — Report as HTML.

You can append :skip-covered to the required report and files with 100% coverage will be ignored.

pytest-timeout

  • --timeout — Change the timeout (set to 0 to disable)

pytest-socket

  • --allow-hosts — List of allowed IP addresses