Registry / testing / pytest-reraise

pytest-reraise

JSON →
library2.1.2pypypi✓ verified 87d ago

pytest-reraise is a pytest plugin designed to make multi-threaded test cases fail when exceptions occur in background threads. Without it, exceptions in threads separate from the main test thread might not be caught by pytest, leading to silently passing tests. The current version is 2.1.2, with a release cadence that focuses on bug fixes and minor feature enhancements.

pip install pytest-reraise
INSTALL
IMPORT
SIG · PYTEST-RERAISE
P
pytest-reraise
testingpythonv2.1.2
Install
2.7s avg
Import
433ms
Disk
29MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.2 · 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.456s · 30.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.7s · import 0.409s · 31MB
29MB installed
● package 29MB
Code
Verified usage

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

Reraise
from pytest_reraise import Reraise
The primary class for managing re-raising exceptions from threads.

This quickstart demonstrates how to use `pytest-reraise` to catch exceptions that occur in background threads. Without `Reraise.wrap()`, `pytest` would typically miss these exceptions, allowing the test to pass even if a critical error occurred in a non-main thread.

import threading from pytest_reraise import Reraise import pytest def test_threaded_exception_is_caught(): # This test will fail due to the ValueError in the thread # because pytest-reraise ensures the exception is re-raised. # Option 1: Instantiate Reraise reraise_instance = Reraise() def func_with_error(): raise ValueError("This exception should fail the test!") thread_with_error = threading.Thread(target=reraise_instance.wrap(func_with_error)) thread_with_error.start() thread_with_error.join() # Option 2: Use the class method (introduced in v2.1.0) def another_func_with_error(): # Example of a division by zero _ = 1 / 0 thread_with_another_error = threading.Thread(target=Reraise.wrap(another_func_with_error)) thread_with_another_error.start() thread_with_another_error.join() # To run this example: # 1. Save it as `test_example.py` # 2. Run `pytest test_example.py` in your terminal. # Expected outcome: Both parts of the test will raise exceptions and cause the test to fail.
pytest --version
Debug
Known issues
breakingPython 3.5 support was officially dropped in version 2.0.0. If you are using an older Python version, you must either upgrade Python or pin `pytest-reraise` to `<2.0.0`.
fix
Upgrade Python to 3.6.1 or newer, or downgrade `pytest-reraise` to version `1.0.3` (`pip install "pytest-reraise<2.0.0"`).
affects: <2.0.0
gotchapytest-reraise only intercepts exceptions from functions explicitly wrapped with `Reraise.wrap()` (or an instance's `wrap()` method). It does not globally monkey-patch all thread exceptions.
fix
Ensure that any function intended to run in a separate thread and whose exceptions you want pytest to catch is wrapped using `Reraise.wrap(your_function)` before passing it to `threading.Thread(target=...)`.
affects: All
gotchaAn `AttributeError` could occur when `pytest-reraise` is used alongside non-test plugins like `pytest-black` or `pytest-flake8`.
fix
Upgrade `pytest-reraise` to version 2.1.1 or newer. This issue was fixed in version 2.1.1.
affects: <2.1.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pytest_reraise'
The `pytest-reraise` package is not installed in your current Python environment.
fix
Run `pip install pytest-reraise` to install the package.
SyntaxError: invalid syntax (on Python 3.5 or older after upgrading to pytest-reraise 2.x)
`pytest-reraise` dropped support for Python 3.5 in version 2.0.0, requiring Python 3.6.1 or newer.
fix
Upgrade your Python interpreter to 3.6.1 or newer, or downgrade `pytest-reraise` to a compatible version: `pip install "pytest-reraise<2.0.0"`.
Test passes unexpectedly despite exception in a background thread.
The function executing in the background thread was not properly wrapped with `pytest_reraise.Reraise.wrap()` or an instance's `wrap()` method.
fix
Modify your `threading.Thread` target to explicitly wrap the function: `threading.Thread(target=Reraise.wrap(your_function_that_raises))`.
Upgrade
Version history
2.1.2latest on PyPI · released Sep 20, 2022
Audit
Dependencies
pytestrequiredCore testing framework pytest-reraise extends.
Agent activity
8 hits · last 30 days
node
8
Resources
pytest-reraise — pip install pytest-reraise · libregistry