skillfed

pytest-memray

A simple plugin to use with pytest

pytest-memray v1.10.0 2.9M downloads/30d#2,836 on PyPI422
Permissive license Apache-2.0 Active released

What it is and what it does

pytest-memray is a pytest plugin that wraps memray's memory profiling capabilities for test-driven memory debugging. It activates via the `--memray` CLI flag and tracks heap allocations throughout each test, then generates a summary report showing total memory used, allocation counts, and the call stacks responsible for the largest allocations. You can set per-test memory limits using the `limit_memory` marker and fail tests that exceed them; the plugin also supports tracking memory growth across runs with `--fail-on-increase` to catch regressions.

The plugin is designed for developers who need to catch memory leaks or excessive allocations in their test suite without manually instrumenting code. It outputs allocation histograms, identifies the functions and line numbers responsible for the biggest memory consumers, and can optionally show native C-level frames for deeper profiling. Configuration is available both via CLI flags and pytest INI settings.

Use it for:

  • Catch memory leaks in unit tests by setting per-test memory limits and failing tests that exceed them.
  • Identify which functions allocate the most memory during test execution for optimization.
  • Track memory usage trends across test runs and fail if a test's memory footprint grows unexpectedly.
  • Debug memory issues in CI/CD pipelines by generating detailed allocation reports for each test.
  • Profile Python allocator behavior separately from native allocations using `--trace-python-allocators`.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

A pytest plugin that integrates memray memory profiling into test runs, tracking memory allocations per test and reporting detailed allocation breakdowns.

Yes. The plugin is actively maintained, has low install friction, carries no security vulnerabilities, and solves a real problem—memory profiling in tests—that is otherwise tedious to set up. It's well-suited for projects concerned with memory efficiency or regression detection. The Apache-2.0 license is permissive and poses no barrier to adoption.

Install

pytest-memray on PyPI

pip

pip install pytest-memray

uv

uv add pytest-memray

poetry

poetry add pytest-memray

Installing pytest-memray

Before you install

Low friction install with a pure-Python wheel. Actively maintained with a release within the past week. Requires only memray and pytest as runtime dependencies, both widely available.

License in practice

Apache-2.0 permissive license allows use in commercial and private projects with minimal restrictions; you must include a copy of the license.

Quickstart

pip install pytest-memray

pytest --memray tests/

# In a test file:
import pytest

@pytest.mark.limit_memory("100.0KiB")
def test_my_function():
    data = [i for i in range(10)]
    assert len(data) == 10

Requires Python 3.8 or higher; memray itself only runs on Linux and macOS.

Verify before relying

  • Whether the plugin works with pytest fixtures and parametrized tests without additional configuration.
  • Performance overhead of native frame tracking and Python allocator tracing modes on large test suites.
  • Compatibility with other pytest plugins that also instrument memory or test execution.

Package facts

License Apache-2.0 (permissive)
Python support supports the current Python release (>=3.8)
Install friction low — pure-Python wheel
Runtime dependencies 2 — memray, pytest
Maintenance actively maintained — 7 days since the last release
Last repo commit
First released
Downloads 2,893,206/month — #2,836 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: pytest_memray-1.10.0-py3-none-any.whl

Intended Audience :: DevelopersLicense :: OSI Approved :: Apache Software LicenseOperating System :: MacOSOperating System :: POSIX :: LinuxProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.15Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonTopic :: Software Development :: Debuggers

Tags

pytest memory profiling plugintest memory trackingmemory allocation debuggingpytest memray integrationmemory limit testingallocation tracking pytestmemory regression detection
memory-profilingpytest-pluginperformance-testing

More Debuggers packages