pytest-memray#

A pytest plugin for easy integration of memray in your test suite.

It can produce reports like:

$ python3 -m pytest tests --memray
=============================== test session starts ================================
platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /mypackage, configfile: pytest.ini
plugins: cov-2.12.0, memray-0.1.0
collected 21 items

tests/test_package.py .....................                                   [100%]


================================= MEMRAY REPORT ==================================
Allocations results for tests/test_package.py::some_test_that_allocates

     📦 Total memory allocated: 24.4MiB
     📏 Total allocations: 33929
     📊 Histogram of allocation sizes: |▂   █    |
     🥇 Biggest allocating functions:
        - parse:/opt/bb/lib/python3.8/ast.py:47 -> 3.0MiB
        - parse:/opt/bb/lib/python3.8/ast.py:47 -> 2.3MiB
        - _visit:/opt/bb/lib/python3.8/site-packages/astroid/transforms.py:62 -> 576.0KiB
        - parse:/opt/bb/lib/python3.8/ast.py:47 -> 517.6KiB
        - __init__:/opt/bb/lib/python3.8/site-packages/astroid/node_classes.py:1353 -> 512.0KiB

Installation#

This plugin can be installed using pip:

$ pip install pytest-memray

Usage#

pytest-memray is a pytest plugin. It is enabled when you pass --memray to pytest:

$ python3.9 -m pytest tests/ --memray

Allocation tracking#

By default, the plugin will track allocations in all tests. This information is reported after tests run ends:

.. example report starts
$ python3 -m pytest tests --memray
=============================== test session starts ================================
platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /mypackage, configfile: pytest.ini
plugins: cov-2.12.0, memray-0.1.0
collected 21 items

tests/test_package.py .....................                                   [100%]


================================= MEMRAY REPORT ==================================
Allocations results for tests/test_package.py::some_test_that_allocates

     📦 Total memory allocated: 24.4MiB
     📏 Total allocations: 33929
     📊 Histogram of allocation sizes: |▂   █    |
     🥇 Biggest allocating functions:
        - parse:/opt/bb/lib/python3.8/ast.py:47 -> 3.0MiB
        - parse:/opt/bb/lib/python3.8/ast.py:47 -> 2.3MiB
        - _visit:/opt/bb/lib/python3.8/site-packages/astroid/transforms.py:62 -> 576.0KiB
        - parse:/opt/bb/lib/python3.8/ast.py:47 -> 517.6KiB
        - __init__:/opt/bb/lib/python3.8/site-packages/astroid/node_classes.py:1353 -> 512.0KiB

Markers#

This plugin provides markers that can be used to enforce additional checks and validations on tests when this plugin is enabled.

Important

These markers do nothing when the plugin is not enabled.

limit_memory#

When this marker is applied to a test, it will cause the test to fail if the execution of the test allocates more memory than allowed. It takes a single argument with a string indicating the maximum memory that the test can allocate.

The format for the string is <NUMBER> ([KMGTP]B|B). The marker will raise ValueError if the string format cannot be parsed correctly.

Warning

As the Python interpreter has its own object allocator is possible that memory is not immediately released to the system when objects are deleted, so tests using this marker may need to give some room to account for this.

Example of usage:

@pytest.mark.limit_memory("24 MB")
def test_foobar():
    # do some stuff that allocates memory

Fixtures#

None provided at this time.

Configuration#

This plugin provides a clean minimal set of command line options that are added to pytest.

Reference#

The complete list of command line options is:

--memray

Activate memray tracking.

--most-allocations=MOST_ALLOCATIONS

Show the N tests that allocate most memory (N=0 for all).

--hide-memray-summary

Hide the memray summary at the end of the execution.

--memray-bin-path

Path where to write the memray binary dumps (by default a temporary folder).

memray(bool)

Activate memray tracking.

most-allocations(string)

Show the N tests that allocate most memory (N=0 for all).

hide_memray_summary(bool)

Hide the memray summary at the end of the execution.

Release History#

v1.1.0 (2022-05-17)#

Features - 1.1.0#

  • Report memory limit and allocated memory in longrepr - by @petr-tik. (#5)

  • Allow passing --memray-bin-path argument to the CLI to allow persisting the binary dumps - by @gaborbernat. (#10)

  • Release a pure python wheel - by @gaborbernat. (#11)

  • Switch build backend from setuptools to hatchling - by @gaborbernat. (#12)

Bug Fixes - 1.1.0#

  • Causes built-in junit-xml results writer to fail - by @petr-tik. (#3)

Improved Documentation - 1.1.0#

  • Move documentation from Github Pages to readthedocs - by @gaborbernat. (#20)

v1.0.0 (2022-04-09)#

  • Initial release.