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 pytestVerified import paths — ran on the pinned version, not inferred.
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.
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).
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`.
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.*'`.