Registry / testing / pytest-profiling

pytest-profiling

JSON →
library1.8.1pypypi✓ verified 26d ago

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-profiling
INSTALL
IMPORT
SIG · PYTEST-PROFILING
P
pytest-profiling
testingpythonv1.8.1
Install
2.8s avg
Import
—
Disk
30MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v1.8.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 31.5MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 2.8s · import 0.000s · 32MB
30MB installed
● package 30MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytest_plugins
✓ pytest_plugins = ['pytest_profiling'] # in conftest.py
While typically auto-discovered, explicitly enabling the plugin in conftest.py with `pytest_plugins = ['pytest_profiling']` ensures it's loaded, especially if not using setuptools entry points for discovery. No direct Python imports are usually needed for the plugin itself during test execution.

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.

import pytest def test_slow_function(): total = 0 for i in range(100000): total += i * i assert total > 0 # Run from command line: # pytest --profile # pytest --profile-svg # Example of running the command: # import subprocess # import os # # # Create a dummy test file # test_file_content = """ # import time # def test_example_profile(): # time.sleep(0.01) # Simulate some work # x = 0 # for i in range(1000): # x += i # assert x == 499500 # """ # with open("test_example_profile.py", "w") as f: # f.write(test_file_content) # # print("\n--- Running pytest --profile ---") # profile_cmd = ["pytest", "--profile", "test_example_profile.py"] # result = subprocess.run(profile_cmd, capture_output=True, text=True, check=False) # print(result.stdout) # if result.stderr: print(result.stderr) # # # Optional: Clean up # # os.remove("test_example_profile.py") # # import shutil # # if os.path.exists("prof"): shutil.rmtree("prof")
pytest --version
Debug
Known issues
breakingVersion 1.8.0 and later dropped support for Python 2 and Python versions less than 3.6. Ensure your environment is running Python 3.6 or newer.
fix
Upgrade your Python environment to 3.6 or a later compatible version.
affects: >=1.8.0
gotchaGenerating SVG profile graphs with `--profile-svg` requires external tools: `gprof2dot` (Python package) and the `dot` command-line utility from Graphviz. These must be installed separately from `pytest-profiling`.
fix
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.
affects: All versions
gotchaBy default, `pytest-profiling` might not extensively profile the setup/teardown phases of pytest fixtures, focusing primarily on the test function execution itself. This can lead to incomplete profiling results if significant time is spent within fixtures.
fix
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.
affects: All versions
gotchaThe raw profiling output can be very verbose, often showing many internal `pytest` and library calls. Analyzing specific bottlenecks in your application code may require filtering the `pstats` output programmatically.
fix
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.
affects: All versions
Upgrade
Version history
1.8.1latest on PyPI · released Nov 29, 2024
Audit
Dependencies
pytestrequiredCore testing framework that this plugin extends.
gprof2dotoptionalRequired for generating SVG call-graph visualizations with `--profile-svg`.
graphvizoptionalThe 'dot' command from Graphviz is required by gprof2dot to render SVG graphs.
Agent activity
23 hits · last 30 days
node
18
OpenAI (training)
2
Resources
pytest-profiling — pip install pytest-profiling · libregistry