Skip to main content

Detects thread leaks

Project description

Current version Supports Python 2.7, 3.10, 3.9, 3.8, 3.7, 3.7 Downloads per month Tests status MIT license

Detects tests leaking threads


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

$ pip install pytest-threadleak


The threadleak pytest plugin will fail leaking threads. This can be an issue in the test, or in the tested code.

Here is an example leaking test:

$ cat
import threading
import time

def test_leak():
    threading.Thread(target=time.sleep, args=(1,)).start()

Here is an example run with thread leak plugin enabled:

$ pytest --threadleak
>   ???
E   Failed: Test leaked [<Thread(Thread-1, started 139762716391168)>]

If you want to enable thread leak by default, you can enable it in your pytest.ini or tox.ini:

threadleak = True

If you want to enable thread leak on a per test/module basis, you can use the threadleak pytest marker:

To enable it for a single test:

def test_leak():

To disable it for a single test:

def test_leak():

For an entire test module:

import pytest

pytestmark = pytest.mark.threadleak(enabled=False)

If you want to exclude some threads from the leak check, you can specify a regex to match excluded thread names:

threadleak = True
threadleak_exclude = pool/\d+


Running the tests:

$ tox


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


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

Download files

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

Source Distribution

pytest-threadleak-0.5.0.tar.gz (4.3 kB view hashes)

Uploaded source

Built Distribution

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