{"id":3244,"library":"pytest-profiling","title":"Pytest Profiling Plugin","description":"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).","status":"active","version":"1.8.1","language":"en","source_language":"en","source_url":"https://github.com/man-group/pytest-plugins","tags":["pytest","profiling","performance","testing","plugin"],"install":[{"cmd":"pip install pytest-profiling","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core testing framework that this plugin extends.","package":"pytest","optional":false},{"reason":"Required for generating SVG call-graph visualizations with `--profile-svg`.","package":"gprof2dot","optional":true},{"reason":"The 'dot' command from Graphviz is required by gprof2dot to render SVG graphs.","package":"graphviz","optional":true}],"imports":[{"note":"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.","symbol":"pytest_plugins","correct":"pytest_plugins = ['pytest_profiling'] # in conftest.py"}],"quickstart":{"code":"import pytest\n\ndef test_slow_function():\n    total = 0\n    for i in range(100000):\n        total += i * i\n    assert total > 0\n\n# Run from command line:\n# pytest --profile\n# pytest --profile-svg\n\n# Example of running the command:\n# import subprocess\n# import os\n# \n# # Create a dummy test file\n# test_file_content = \"\"\"\n# import time\n# def test_example_profile():\n#     time.sleep(0.01) # Simulate some work\n#     x = 0\n#     for i in range(1000):\n#         x += i\n#     assert x == 499500\n# \"\"\"\n# with open(\"test_example_profile.py\", \"w\") as f:\n#     f.write(test_file_content)\n# \n# print(\"\\n--- Running pytest --profile ---\")\n# profile_cmd = [\"pytest\", \"--profile\", \"test_example_profile.py\"]\n# result = subprocess.run(profile_cmd, capture_output=True, text=True, check=False)\n# print(result.stdout)\n# if result.stderr: print(result.stderr)\n# \n# # Optional: Clean up\n# # os.remove(\"test_example_profile.py\")\n# # import shutil\n# # if os.path.exists(\"prof\"): shutil.rmtree(\"prof\")","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.6 or a later compatible version.","message":"Version 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.","severity":"breaking","affected_versions":">=1.8.0"},{"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.","message":"Generating 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`.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"By 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}