Pytest Profiling Plugin

1.8.1 · active · verified Sat Apr 11

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).

Warnings

Install

Imports

Quickstart

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")

view raw JSON →