Skip to main content

A pytest plugin to trace resource leaks.

Project description

pytest-leaks - A pytest plugin to trace resource leaks

See Package Info on PyPI See Build Status on Travis CI See Build Status on AppVeyor

A pytest plugin to trace resource leaks.

Usage

leaks:
  -R LEAKS, --leaks=LEAKS
                        runs each test several times and examines
                        sys.gettotalrefcount() to see if the test appears to
                        be leaking references. The argument should be of the
                        form stab:run where 'stab' is the number of times the
                        test is run to let gettotalrefcount settle down, 'run'
                        is the number of times further it is run. These
                        parameters all have defaults (5 and 4, respectively),
                        and the minimal invocation is '-R :'.

To add a leaks test to your py.test session, add the -R option on the command line:

$ cd examples; pytest-3 -v -R : test_faucet.py
=========================== test session starts ===========================
platform linux -- Python 3.7.4, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 --
cachedir: .pytest_cache
rootdir: ..
plugins: leaks-0.3.0
collected 3 items

test_faucet.py::test_leaky_faucet LEAKED                            [ 33%]
test_faucet.py::test_broken_faucet FAILED                           [ 66%]
test_faucet.py::test_mended_faucet PASSED                           [100%]

================================ FAILURES =================================
___________________________ test_broken_faucet ____________________________

    def test_broken_faucet():
>       assert 0
E       assert 0

test_faucet.py:6: AssertionError
============================== leaks summary ==============================
examples/test_faucet.py::test_leaky_faucet: leaked references: [2, 2, 2, 2], memory blocks: [2, 2, 2, 2]
============== 1 failed, 1 passed, 1 leaked in 0.32 seconds ===============

The test file used above contains the following code:

$ cat test_faucet.py
drops = []
def test_leaky_faucet():
    drops.append({})

def test_broken_faucet():
    assert 0

def test_mended_faucet():
    assert 1

Note that pytest-leaks runs tests several times: if you see test failures that are present only when using pytest-leaks, check that the test does not modify any global state in a way that prevents it from running a second time.

Features

  • Detects memory leaks by running py.test tests repeatedly and comparing total reference counts between the runs.

Requirements

  • py.test version >= 3;
  • A debug build of Python (2.7 or >=3.5).

On Linux, Python debug builds can be found in packages pythonX.Y-dbg (Debian and derivatives) and python3-debug (Fedora and derivatives).

Installation

You can install "pytest-leaks" via pip from PyPI:

$ pip install pytest-leaks

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT and PSF licenses, "pytest-leaks" is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Acknowledgements

This Pytest plugin was initially generated with Cookiecutter along with @hackebrot's Cookiecutter-pytest-plugin template.

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-leaks-0.3.0.tar.gz (17.2 kB view details)

Uploaded Source

File details

Details for the file pytest-leaks-0.3.0.tar.gz.

File metadata

  • Download URL: pytest-leaks-0.3.0.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for pytest-leaks-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1834360b92ba7f5ac6a7fc993e444832de93c1f592664ddc539752288332011f
MD5 d6c91d6e80e63e1a4e940a7d725355ba
BLAKE2b-256 c6bfb1db4556c2c1a39fef3a59335fbb0d26fabb21ec4b0af86fa12498c96f08

See more details on using hashes here.

Provenance

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