pytest-memray

1.8.0 · active · verified Sat Apr 11

pytest-memray is a pytest plugin for easy integration of memray, a powerful memory profiler for Python. It allows developers to analyze memory allocations, detect memory leaks, and identify memory-intensive hotspots directly within their pytest test suite. The current version, 1.8.0, supports Python 3.8 and higher, and is actively maintained with regular releases.

Warnings

Install

Imports

Quickstart

To activate `pytest-memray` during a test run, simply add the `--memray` flag to your `pytest` command. For granular control and enforcement within tests, use markers like `@pytest.mark.limit_memory` to set a peak memory limit or `@pytest.mark.limit_leaks` to detect memory leaks. If a test exceeds its limit, `pytest` will report a failure.

import pytest

@pytest.mark.limit_memory('24 MB')
def test_foobar():
    data = [i for i in range(1_000_000)] # Allocates ~8MB
    assert len(data) == 1_000_000

# To run this test with memory profiling:
# pytest --memray your_test_file.py

view raw JSON →