Skip to main content

A simple plugin to use with pytest

Project description

logo

pytest-memray

PyPI - Python Version PyPI - Implementation PyPI PyPI - Downloads Tests Code Style

pytest-memray is a pytest plugin for easy integration of memray.

Installation

pytest-memray requires Python 3.7+ and can be easily installed using most common Python packaging tools. We recommend installing the latest stable release from PyPI with pip:

pip install pytest-memray

Documentation

You can find the latest documentation available here.

Usage

To use the plugin in a pytest run, simply add --memray to the command line invocation:

pytest --memray tests

Would produce a report 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

Configuration

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

  • --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.

Markers

There are some builtin markers and fixtures in pytest-memray:

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.

Example of usage:

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

License

pytest-memray is Apache-2.0 licensed, as found in the LICENSE file.

Code of Conduct

This project has adopted a Code of Conduct. If you have any concerns about the Code, or behavior which you have experienced in the project, please contact us at opensource@bloomberg.net.

Security Policy

If you believe you have identified a security vulnerability in this project, please send email to the project team at opensource@bloomberg.net, detailing the suspected issue and any methods you've found to reproduce it.

Please do NOT open an issue in the GitHub repository, as we'd prefer to keep vulnerability reports private until we've had an opportunity to review and address them.

Contributing

We welcome your contributions to help us improve and extend this project!

Below you will find some basic steps required to be able to contribute to the project. If you have any questions about this process or any other aspect of contributing to a Bloomberg open source project, feel free to email opensource@bloomberg.net, and we'll get your questions answered as quickly as we can.

Contribution Licensing

Since this project is distributed under the terms of an open source license, contributions that you make are licensed under the same terms. In order for us to be able to accept your contributions, we will need explicit confirmation from you that you are able and willing to provide them under these terms, and the mechanism we use to do this is called a Developer's Certificate of Origin (DCO). This is very similar to the process used by the Linux(R) kernel, Samba, and many other major open source projects.

To participate under these terms, all that you must do is include a line like the following as the last line of the commit message for each commit in your contribution:

Signed-Off-By: Random J. Developer <random@developer.example.org>

The simplest way to accomplish this is to add -s or --signoff to your git commit command.

You must use your real name (sorry, no pseudonyms, and no anonymous contributions).

Steps

  • Create an Issue, selecting 'Feature Request', and explain the proposed change.
  • Follow the guidelines in the issue template presented to you.
  • Submit the Issue.
  • Submit a Pull Request and link it to the Issue by including "#" in the Pull Request summary.

Development

The project requires a Linux OS to work. To set up a DEV environment use tox (or directly the make targets). You can use Docker to run the test suite on non Linux as in (you can parametrize tox by passing additional arguments at the end):

docker-compose run --rm test tox

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_memray-1.1.0.tar.gz (234.0 kB view details)

Uploaded Source

Built Distribution

pytest_memray-1.1.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest_memray-1.1.0.tar.gz.

File metadata

  • Download URL: pytest_memray-1.1.0.tar.gz
  • Upload date:
  • Size: 234.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for pytest_memray-1.1.0.tar.gz
Algorithm Hash digest
SHA256 9d7317e5a518d61cd9e9a3cbeb64a4db903c25aefe86bff2b748f96ca3d7a58a
MD5 d886ca60d8deffe463a23d94442ac52b
BLAKE2b-256 c73a4c977be57e6a5b2d6e5077198cd6f318dee75663cdf0e1f9994cb6128ed0

See more details on using hashes here.

File details

Details for the file pytest_memray-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_memray-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6184929a416351b4d0c39a1371bb17fa248a4209b9bbfe78a30d55e5bdfc6036
MD5 14771c9ebce7c5df0020837d36cdb4aa
BLAKE2b-256 91c120f3b7a8c9f5eed16cf62281fc65cc5af8dca487bb5b998e4f44167362f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page