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.
pip install pytest-memrayVerified import paths — ran on the pinned version, not inferred.
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.
Account for Python and pytest internal allocations. Run tests in a loop or with `current_thread_only=True` in markers for clearer leak detection. Consider `memray`'s own documentation on 'false positives'.
Run tests and profiling on Linux or macOS environments for optimal results and full feature access.
Upgrade to `pytest-memray` version 1.3.2 or newer to ensure proper compatibility with `pytest-xdist`.
Keep test file paths and test function names reasonably short, especially if encountering `OSError` related to filename length.
Upgrade to `pytest-memray` 1.6.0 or newer and consider adding `current_thread_only=True` to your markers for more focused memory tracking, if appropriate for your tests.