Skip to main content

A simple plugin to use with pytest

Project description

logo

pytest-memray

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

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

Installation

pytest-memray requires Python 3.8 or higher 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.

Quick introduction

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

pytest --memray tests

After the test suite runs you'll see a memory report printed:

=================================== test session starts ====================================
platform linux -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0
cachedir: /v/.pytest_cache
rootdir: /w
plugins: memray-1.1.0
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

====================================== 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: 54.9KiB
	 📏 Total allocations: 71
	 📊 Histogram of allocation sizes: |       |
	 🥇 Biggest allocating functions:
		- test_track:/w/demo/test_ok.py:12 -> 39.1KiB
		- _compile_bytecode:<frozen importlib._bootstrap_external>:672 -> 7.2KiB
		- _call_with_frames_removed:<frozen importlib._bootstrap>:241 -> 4.7KiB
		- _call_with_frames_removed:<frozen importlib._bootstrap>:241 -> 1.8KiB
		- _is_marked_for_rewrite:/v/lib/python3.10/site-packages/_pytest/assertion/rewrite.py:240 -> 1.1KiB


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

Configuration - CLI flags

  • --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-bin-prefix - prefix to use for the binary dump (by default a random UUID4 hex)

Configuration - INI

  • 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

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.3.2.tar.gz (233.8 kB view details)

Uploaded Source

Built Distribution

pytest_memray-1.3.2-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_memray-1.3.2.tar.gz
  • Upload date:
  • Size: 233.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for pytest_memray-1.3.2.tar.gz
Algorithm Hash digest
SHA256 8835d4618d0a1e04925dbe20d0650a67838a6af7f586c7653b077a888c9a8711
MD5 d046a4e73805bb3268a14ffd1f61556c
BLAKE2b-256 1315ee0da02dd5437e71847bce946cd8d3339f6fa6dad355da5f5053f9de134d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_memray-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 56e80c532d943e2c3591d14e13723b43ed8176a06a2e8cdc919ca8f1f77c4466
MD5 d9e5906cc5a725617f36fd31e5697586
BLAKE2b-256 723ba3e69ada0d898fb5ecbdd4b1e75e822f8cb35fa6b7de68b7e4e1b038c6b1

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