Registry / testing / pytest-threadleak

pytest-threadleak

JSON →
library0.5.0pypypiunverified

pytest-threadleak is a pytest plugin that automatically detects non-daemon thread leaks after tests complete. It helps identify when tests leave background threads running, which can cause resource exhaustion or unexpected behavior. The current version is 0.5.0, and it is maintained with releases as needed for bug fixes or new features.

pip install pytest-threadleak pytest
INSTALL
IMPORT
SIG · PYTEST-THREADLEAK
P
pytest-threadleak
testingpythonv0.5.0
Install
2.6s avg
Import
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 30.7MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.6s · import 0.000s · 31MB
29MB installed
● package 29MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

threadleak
def test_something(threadleak): ...
from pytest_threadleak import threadleak
pytest-threadleak provides fixtures, not direct module imports for its core functionality. The 'threadleak' fixture is automatically discovered and injected by pytest.

To get started, simply install `pytest-threadleak` and include the `threadleak` fixture in your test functions. The plugin automatically checks for new non-daemon threads spawned during the test that are still active when the test finishes. The example demonstrates a clean test and how to intentionally create a leak to see the plugin in action.

import threading import time # test_my_app.py def test_my_function_no_leak(threadleak): """ This test uses the 'threadleak' fixture to ensure no new non-daemon threads are left running after the test completes. This specific test should pass cleanly. """ def do_something_brief(): time.sleep(0.01) t = threading.Thread(target=do_something_brief) t.start() t.join() # Ensure the thread finishes before the test does # To run this test: # pip install pytest-threadleak pytest # pytest test_my_app.py # To observe a leak (uncomment and run): # def test_my_function_with_leak(threadleak): # """ # This test will intentionally leave a non-daemon thread running, # causing pytest-threadleak to report a ThreadLeakError. # """ # def infinite_loop(): # while True: # time.sleep(0.1) # # threading.Thread(target=infinite_loop).start()
Debug
Known issues
breakingPython 2 support was dropped in version 0.3.0. Users on Python 2 must use an older version.
fix
Upgrade to Python 3 or restrict pytest-threadleak version to 0.2.0 or earlier if Python 2 is strictly required (not recommended for new projects).
affects: <=0.2.0
gotchapytest-threadleak only detects threads that are *still running* and are *not daemon threads* after a test. Threads that complete and join successfully before the test ends will not be flagged. Daemon threads are also ignored by default.
fix
Ensure all non-daemon threads are explicitly joined or terminated within the test's scope. For long-running background threads that are expected, configure `threadleak_thread_name_patterns_to_ignore` in your `pytest.ini`.
affects: All
gotchaIf tests fail with `ThreadLeakError` for threads that are part of standard libraries or external packages (e.g., from `requests` or `concurrent.futures`), these threads might need to be explicitly ignored.
fix
Use the `threadleak_thread_name_patterns_to_ignore` option in your `pytest.ini` to specify regular expressions matching the names of threads that should be ignored, for example: `threadleak_thread_name_patterns_to_ignore = 'ThreadPoolExecutor.*'`.
affects: All
Upgrade
Version history
0.5.0latest on PyPI · released Jul 3, 2022
Audit
Dependencies
pytestrequiredpytest-threadleak is a plugin for the pytest testing framework and requires pytest to function.
Agent activity
11 hits · last 30 days
node
10
Amazon
1
Resources
pytest-threadleak — pip install pytest-threadleak · libregistry