pytest-memray#

A pytest plugin for easy integration of memray in your test suite. It can produce reports like:

$ pytest --memray /w/demo 
=================================== test session starts ====================================
platform linux -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0
rootdir: /w
plugins: memray-1.3.2
collected 2 items

demo/test_ok.py .M                                                                   [100%]

========================================= FAILURES =========================================
____________________________________ test_memory_exceed ____________________________________
Test was limited to 100.0KiB but allocated 117.2KiB
------------------------------------ memray-max-memory -------------------------------------
Test is using 117.2KiB out of limit of 100.0KiB
List of allocations: 
	- <listcomp>:/w/demo/test_ok.py:17 -> 117.2KiB
===================================== warnings summary =====================================
demo/test_ok.py::test_track
  /v/lib/python3.11/site-packages/_pytest/python.py:199: PytestReturnNotNoneWarning: Expected None, but demo/test_ok.py::test_track returned Result(test_id='demo/test_ok.py::test_track', metadata=Metadata(start_time=datetime.datetime(2022, 11, 30, 15, 12, 19, 887000), end_time=datetime.datetime(2022, 11, 30, 15, 12, 19, 894000), total_allocations=3, total_frames=22, peak_memory=40000, command_line='/v/bin/pytest --memray demo', pid=600, python_allocator='pymalloc', has_native_traces=False), result_file=PosixPath('/tmp/tmpozsfddno/e736222448794788b19a76ea0e95268d.bin')), which will be an error in a future version of pytest.  Did you mean to use `assert` instead of `return`?
    warnings.warn(

demo/test_ok.py::test_memory_exceed
  /v/lib/python3.11/site-packages/_pytest/python.py:199: PytestReturnNotNoneWarning: Expected None, but demo/test_ok.py::test_memory_exceed returned Result(test_id='demo/test_ok.py::test_memory_exceed', metadata=Metadata(start_time=datetime.datetime(2022, 11, 30, 15, 12, 19, 896000), end_time=datetime.datetime(2022, 11, 30, 15, 12, 19, 902000), total_allocations=30, total_frames=22, peak_memory=120000, command_line='/v/bin/pytest --memray demo', pid=600, python_allocator='pymalloc', has_native_traces=False), result_file=PosixPath('/tmp/tmpozsfddno/d7352ed007fd4d96a4673ffebb2284ec.bin')), which will be an error in a future version of pytest.  Did you mean to use `assert` instead of `return`?
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

====================================== MEMRAY REPORT =======================================
Allocations results for demo/test_ok.py::test_memory_exceed

	 📦 Total memory allocated: 117.2KiB
	 📏 Total allocations: 30
	 📊 Histogram of allocation sizes: |█|
	 🥇 Biggest allocating functions:
		- <listcomp>:/w/demo/test_ok.py:17 -> 117.2KiB


Allocations results for demo/test_ok.py::test_track

	 📦 Total memory allocated: 39.1KiB
	 📏 Total allocations: 3
	 📊 Histogram of allocation sizes: |█|
	 🥇 Biggest allocating functions:
		- test_track:/w/demo/test_ok.py:12 -> 39.1KiB


================================= short test summary info ==================================
MEMORY PROBLEMS demo/test_ok.py::test_memory_exceed
========================= 1 failed, 1 passed, 2 warnings in 0.02s ==========================