|
| 1 | +# pytest-execution-timer |
| 2 | + |
| 3 | +A plugin to use with Pytest to measure execution time of tests. |
| 4 | + |
| 5 | +Distinctly different from the `--durations` option of pytest, |
| 6 | +this plugin measures specific pytest startup/collection phases. |
| 7 | + |
| 8 | +Leverages `pytest` hooks to measure execution time of phases. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Requires: |
| 15 | + |
| 16 | +- Python 3.7 or later. |
| 17 | +- Pytest 6.2 or later. |
| 18 | + |
| 19 | +Install the plugin with any approach for your project. |
| 20 | + |
| 21 | +Some examples: |
| 22 | + |
| 23 | +```shell |
| 24 | +pip install pytest-execution-timer |
| 25 | +``` |
| 26 | + |
| 27 | +```shell |
| 28 | +poetry add --dev pytest-execution-timer |
| 29 | +``` |
| 30 | + |
| 31 | +```shell |
| 32 | +pipenv install --dev pytest-execution-timer |
| 33 | +``` |
| 34 | + |
| 35 | +Or add it to your `requirements.txt` file. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +Enable the plugin with the `--execution-timer` option when running `pytest`: |
| 40 | + |
| 41 | +```console |
| 42 | +$ pytest --execution-timer |
| 43 | +... |
| 44 | +Durations of pytest phases in seconds (min 100ms): |
| 45 | +0.662 pytest_runtestloop |
| 46 | +``` |
| 47 | + |
| 48 | +Control the threshold (default 100ms) by passing `--minimum-duration=<value in ms>`: |
| 49 | + |
| 50 | +```console |
| 51 | +$ pytest --execution-timer --minimum-duration=1000 # 1 second |
| 52 | +``` |
| 53 | + |
| 54 | +## Understanding the output |
| 55 | + |
| 56 | +The best ay to start is to compare the difference of the `pytest_runtestloop` duration |
| 57 | +and the overall duration of the test run. Example: |
| 58 | + |
| 59 | +```console |
| 60 | +Durations of pytest phases in seconds (min 100ms): |
| 61 | +0.666 pytest_runtestloop |
| 62 | +====== 4 passed in 0.68s ====== |
| 63 | +``` |
| 64 | + |
| 65 | +In this example, there's not much lost between the test run and the `pytest_runtestloop` |
| 66 | +meaning that the startup and collection phases are not taking too much time. |
| 67 | + |
| 68 | +If there's a larger difference in the timings, |
| 69 | +look to other emitted phases to understand what's taking the most time. |
| 70 | + |
| 71 | +These can then be examined directly, |
| 72 | +or use other tools like [profilers](https://docs.python.org/3/library/profile.html) |
| 73 | +or [import timings](https://docs.python.org/3/using/cmdline.html#cmdoption-X). |
| 74 | + |
| 75 | +## License |
| 76 | + |
| 77 | +Distributed under the terms of the MIT license, |
| 78 | +"pytest-execution-timer" is free and open source software. |
| 79 | +See `LICENSE` for more information. |
0 commit comments