Post/Code

HomeAboutUsesNow

Useful Pytest Commands.

When working with Pytest, it is tempting to keep re-using the same old commands. However, there are some really useful options you can pass in via the command line in order to change the set of tests being run and most likely speed up your workflow.

Take for example, your basic pytest command as a base:

  • pytest "project/tests"

The above command is running Pytest out of the 'project/tests' folder.

Here are some useful variations to try for running tests:

  • Without warnings: pytest "project/tests" -p no:warnings
  • That failed in last run (extremely useful!): users pytest "project/tests" --lf
  • With local variables in traceback: pytest "project/tests" -l
  • Stop after first failure: pytest "project/tests" -x
  • ...or stop after second failure: pytest "project/tests" --maxfail=2
  • With coverage: pytest "project/tests" -p no:warnings --cov="project"
  • With coverage as an HTML page (remember to exclude /htmlcov from your repo): pytest "project/tests" -p no:warnings --cov="project" --cov-report html
  • With only "registration" in the test name: pytest "project/tests" -k registration
  • List the two slowest tests: pytest "project/tests" --durations=2

Hopefully some of these will help streamline your Pytest-ing workflows!