pytest-profiling is a plugin for the pytest testing framework that provides code profiling capabilities. It generates profiling information in tabular format and can also produce call-graph heat maps in SVG format. The plugin uses Python's built-in `cProfile` module for profiling and `pstats` for analysis, with `gprof2dot` and `dot` (Graphviz) for SVG visualization. It is actively maintained, with regular updates to support recent Python and pytest versions, as seen by its latest releases (v1.8.1 in November 2024 and v1.8.0 in October 2024).
pip install pytest-profilingVerified import paths — ran on the pinned version, not inferred.
After installation, `pytest-profiling` integrates directly with the `pytest` command-line interface. To generate profiling information, use the `--profile` flag. For a visual call-graph (SVG), use `--profile-svg`. This will create a `prof/` directory containing the profiling data and SVG file if `--profile-svg` is used.
Upgrade your Python environment to 3.6 or a later compatible version.
Install `gprof2dot` via pip (`pip install gprof2dot`) and Graphviz (which provides the `dot` command) through your system's package manager or from its official website.
If fixture profiling is crucial, consider explicit timing within fixtures or alternative profiling methods. The plugin focuses on test execution, and extensive fixture profiling might require deeper integration or manual instrumentation.
After running `pytest --profile`, the profiling data is saved to `prof/combined.prof`. You can load this with Python's `pstats` module and use `stats.print_stats('your_module_name', N)` to focus on specific modules or top N calls.