{"id":10148,"library":"pytest-reraise","title":"pytest-reraise","description":"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.","status":"active","version":"2.1.2","language":"en","source_language":"en","source_url":"https://github.com/bjoluc/pytest-reraise","tags":["pytest","testing","concurrency","threading","exceptions"],"install":[{"cmd":"pip install pytest-reraise","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core testing framework pytest-reraise extends.","package":"pytest","optional":false}],"imports":[{"note":"The primary class for managing re-raising exceptions from threads.","symbol":"Reraise","correct":"from pytest_reraise import Reraise"}],"quickstart":{"code":"import threading\nfrom pytest_reraise import Reraise\nimport pytest\n\ndef test_threaded_exception_is_caught():\n    # This test will fail due to the ValueError in the thread\n    # because pytest-reraise ensures the exception is re-raised.\n    \n    # Option 1: Instantiate Reraise\n    reraise_instance = Reraise()\n    def func_with_error():\n        raise ValueError(\"This exception should fail the test!\")\n\n    thread_with_error = threading.Thread(target=reraise_instance.wrap(func_with_error))\n    thread_with_error.start()\n    thread_with_error.join()\n\n    # Option 2: Use the class method (introduced in v2.1.0)\n    def another_func_with_error():\n        # Example of a division by zero\n        _ = 1 / 0\n    \n    thread_with_another_error = threading.Thread(target=Reraise.wrap(another_func_with_error))\n    thread_with_another_error.start()\n    thread_with_another_error.join()\n\n# To run this example:\n# 1. Save it as `test_example.py`\n# 2. Run `pytest test_example.py` in your terminal.\n# Expected outcome: Both parts of the test will raise exceptions and cause the test to fail.","lang":"python","description":"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."},"warnings":[{"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\"`).","message":"Python 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`.","severity":"breaking","affected_versions":"<2.0.0"},{"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=...)`.","message":"pytest-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.","severity":"gotcha","affected_versions":"All"},{"fix":"Upgrade `pytest-reraise` to version 2.1.1 or newer. This issue was fixed in version 2.1.1.","message":"An `AttributeError` could occur when `pytest-reraise` is used alongside non-test plugins like `pytest-black` or `pytest-flake8`.","severity":"gotcha","affected_versions":"<2.1.1"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install pytest-reraise` to install the package.","cause":"The `pytest-reraise` package is not installed in your current Python environment.","error":"ModuleNotFoundError: No module named 'pytest_reraise'"},{"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\"`.","cause":"`pytest-reraise` dropped support for Python 3.5 in version 2.0.0, requiring Python 3.6.1 or newer.","error":"SyntaxError: invalid syntax (on Python 3.5 or older after upgrading to pytest-reraise 2.x)"},{"fix":"Modify your `threading.Thread` target to explicitly wrap the function: `threading.Thread(target=Reraise.wrap(your_function_that_raises))`.","cause":"The function executing in the background thread was not properly wrapped with `pytest_reraise.Reraise.wrap()` or an instance's `wrap()` method.","error":"Test passes unexpectedly despite exception in a background thread."}]}