Pytest Durations
pytest-durations is an active pytest plugin, currently at version 1.6.2, designed to report detailed execution times for both test functions and their associated fixtures. Unlike pytest's built-in `--durations` feature, it separates these timings and offers compatibility with distributed testing frameworks like pytest-xdist and time-traveling packages. The library sees frequent updates, with multiple minor and patch releases occurring throughout the year.
Warnings
- breaking Python 3.9 support was dropped in version 1.6.2. Python 3.8 support was dropped in version 1.5.0. Ensure your Python environment is 3.10 or newer (up to 3.14).
- breaking The 'avg' column in duration reports was renamed to 'med' in version 1.6.0 to more accurately reflect median durations. Scripts or tools parsing the report output by column name will need updating.
- deprecated The command-line option `--pytest-resultlog` was renamed to `--pytest-durations-log` in version 1.6.0 for consistency. While the old option might still work in some versions, it's considered deprecated.
- gotcha In version 1.5.2, the plugin switched from using `time.monotonic()` to `time.time()` to resolve a bug when used in conjunction with the `time-machine` library. If you rely on specific monotonic timing behavior or use `time-machine`, be aware of this change and test your setup accordingly.
- gotcha The plugin's output differs from pytest's built-in `--durations` option. `pytest-durations` aims to provide separate metrics for fixture setup/teardown and test function execution, and supports xdist/time-traveling. Do not confuse the two outputs.
- gotcha The `--pytest-durations-group-by` option was introduced in 1.6.0 (improved in 1.6.1), allowing grouping by module, class, or function. The default is 'function'. Use `--pytest-durations-group-by=legacy` to revert to previous behavior if your parsing depends on it.
Install
-
pip install pytest-durations
Quickstart
import pytest
import time
def test_fast_example():
time.sleep(0.01)
assert True
def test_slow_example_with_fixture(my_fixture):
time.sleep(0.05)
assert my_fixture == "setup_done"
@pytest.fixture
def my_fixture():
time.sleep(0.02)
yield "setup_done"
time.sleep(0.01)
# To run: Save the above as test_durations.py and run 'pytest --pytest-durations=5' in the terminal.