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.
This tool is not for finding memory leaks; even if your code produces no garbage, native call-ins can still leak memory. Garbage in CPython is specifically circular references between Python objects.
If you are interested in optimizing CPython garbage collection or unsure of the difference between garbage and memory leaks, the gc docs and CPython Garbage Collection devguide are excellent resources.
Requirements
Python 3.7+
This plugin is only tested against CPython.
Installation
You can install “pytest-nogarbage” via pip from PyPI:
$ pip install pytest-nogarbage
Usage
Add the nogarbage fixture to your test to ensure it does not produce garbage:
def test_circular(nogarbage):
l1 = []
l2 = [l1]
l1.append(l2)
# ERROR: Garbage collected after test.
def test_collect(nogarbage):
import gc
gc.collect()
# ERROR: Garbage collected during test.
Running the Tests
This project uses nox to test against multiple Python versions:
$ python3 -m venv .venv $ source .venv/bin/activate (.venv) $ pip install nox uv (.venv) $ nox
License
Distributed under the terms of the MIT license, “pytest-nogarbage” is free and open source software.
Release files for pytest-nogarbage 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest_nogarbage-1.0.1.tar.gz | 4.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_nogarbage-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / pytest_nogarbage-1.0.1.tar.gz
| Download URL | pytest_nogarbage-1.0.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e8b2a47b74bab76f6cae0c325066963d0d85ed05c9324ff97ccde6d41a939382
|
|
BLAKE2b-256 checksum How to use checksums |
62cd41b8a2c0b481c3c637e64dd13dccc7d1681784ce4add653a0f8eaa297add
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / pytest_nogarbage-1.0.1-py3-none-any.whl
| Download URL | pytest_nogarbage-1.0.1-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a4f48bed25946b0d0436d0a90d8deeb88d99b68ec3b492aee0627db9d65cfdb1
|
|
BLAKE2b-256 checksum How to use checksums |
874f5158a4436a6c9cded1efc6be99ac770009d3729ea09cf7f1188329f4d8bb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|