Skip to main content

Ensure a test produces no garbage

Project description

PyPI version Python versions See Build Status on Travis CI

The purpose of this plugin is ensuring that portions of your Python code do not produce garbage or manually invoke garbage collection.

When the nogarbage pytest fixture is added to a test, it will cause an error under the following conditions:

  • Garbage was collected after the test was run (circular references were remaining after the test was broken down).

  • Garbage was explicitly collected (gc.collect()) during the test. Automatic garbage collection sweeps are disabled during the test.

You should use this plugin when:

  • You intend to run a program with garbage collection disabled in production.

  • You wish to verify elimination of garbage collection overhead produced by frequently called functions.


  • Python 3.7+

  • This plugin is only tested against CPython.


You can install “pytest-nogarbage” via pip from PyPI:

$ pip install pytest-nogarbage


Add the nogarbage fixture to your test to ensure it does not produce garbage:

def test_circular(nogarbage):
    l1 = []
    l2 = [l1]
    # ERROR: Garbage collected after test.

def test_collect(nogarbage):
    import gc
    # ERROR: Garbage collected during test.


Distributed under the terms of the MIT license, “pytest-nogarbage” is free and open source software.

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-nogarbage-1.0.0.tar.gz (4.4 kB view hashes)

Uploaded source

Built Distribution

pytest_nogarbage-1.0.0-py3-none-any.whl (4.0 kB view hashes)

Uploaded py3

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