I run pytest a lot. Here are the flags I use most.
My development workflow Link to heading
When I’m writing code, I typically:
- Write the test first (or alongside the code)
- Run just that test:
pytest tests/api/test_order_api.py::test_create_order -v - Fix failures, re-run with
-lfto only run failed tests - Once passing, run the full suite to check for regressions
For debugging, I always use -s -v --tb=short to see print output, verbose names, and shorter tracebacks.
Common flags Link to heading
Run a specific test file:
pytest tests/api/test_order_api.py
Run a specific test function:
pytest tests/api/test_order_api.py::test_create_order
Run only the tests that failed last time:
pytest --last-failed
# or shorter:
pytest -lf
When debugging, I often want to see the print statements:
pytest -s tests/api/test_order_api.py
Stop on the first failure:
pytest -x
Combine them when you’re fixing a bunch of broken tests:
pytest --last-failed -x
Useful plugins Link to heading
- pytest-cov - Coverage reports (
pytest --cov=src) - pytest-xdist - Parallel test execution (
pytest -n auto) - pytest-asyncio - Async test support (see pytest-asyncio mode for configuration)
- pytest-mock - Cleaner mocking with
mockerfixture
For large test suites, pytest-xdist makes a huge difference. Running tests in parallel can cut runtime from minutes to seconds.
Further reading Link to heading
- pytest documentation - comprehensive official docs
- pytest best practices - project structure and conventions