Profilling pytest tests and fixtures

pytest –durations Link to heading

--durations=<n> outputs the top slowest tests/fixtures

pytest --durations=<n>

pyinstrument Link to heading

pyinstrument · PyPI

pip install pyinstrument
pyinstrument -m pytest

pytest-profiling Link to heading

pytest-profiling · PyPI

pip install pytest-profiling

+ SnakeViz Link to heading

snakeviz · PyPI

pytest --profile
pip install snakeviz
snakeviz prof/combined.prof

+ SVG Link to heading

pytest --profile-svg
# open prof/combined.svg

Workaround to time fixtures teardown Link to heading

  1. Edit file <venv>/lib/python<version>/site-packages/_pytest/fixtures.py
  2. Search for def _teardown_yield_fixture
  3. Replace next(it) with
    from time import time
    t = time()
    next(it)
    print(f"#################################### fixture {fixturefunc} teardown took {d} seconds")