pytest¶
"The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries." - https://docs.pytest.org
Common args¶
pytest --help is a bit overwhelming, so here's a smaller reference:
--pdbdrop to pdb when an exception is raised--maxfail=Nquit after this many test failures--ffrun previously failed tests first--lfonly run tests that previously failed-k searchstringonly run tests that have "searchstring' in them (actually more complicated matches can be done with-k)-salias for--capture=nowhich basically means "show output of print statements in tests"
Usage Tips¶
Debug failing test with pdb¶
This will drop you into a pdb shell when a test failure occurs.
Override test args¶
Run only tests that failed on the last run¶
Run all tests, but put the last failures first¶
Run a specific test case¶
You can use python expressions to match more than one test. Each given test is substring matched against available tests. The matching logic can get pretty complicated, so see the help docs.
Passing args via ENV vars¶
You can pass args via the PYTEST_ADDOPTS ENV var. This is useful for instance if you're using make to run tests, and the command line does additional things like source files, enter a venv, or whatever.
Show your fixtures¶
This shows all fixtures that are available for use in your tests.
Show fixtures per test¶
This shows a list of functions, the files they are in, and which fixtures the function uses.
Show fixture setup and teardown during run¶
Plugins¶
- pytest-profiling: "Profiling plugin for pytest, with tabular and heat graph output."
- pytest-sugar: improved display of test output
- pytest-xdist: parallel runs of tests for speed improvements
- pytest-testmon: "selects tests affected by changed files and methods"