{"id":24288172,"url":"https://github.com/bjoluc/pytest-reraise","last_synced_at":"2025-09-10T02:42:41.100Z","repository":{"id":49438190,"uuid":"264298765","full_name":"bjoluc/pytest-reraise","owner":"bjoluc","description":"Make multi-threaded pytest test cases fail when they should","archived":false,"fork":false,"pushed_at":"2023-02-06T14:33:01.000Z","size":88,"stargazers_count":18,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-14T00:50:25.554Z","etag":null,"topics":["assert","catch","exception","plugin","pytest","raise","re-raise","threads"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bjoluc.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-15T21:05:33.000Z","updated_at":"2025-01-13T19:14:07.000Z","dependencies_parsed_at":"2024-06-19T09:54:28.490Z","dependency_job_id":"f584b405-b83d-4283-850a-d1ac0c61a0ea","html_url":"https://github.com/bjoluc/pytest-reraise","commit_stats":{"total_commits":36,"total_committers":5,"mean_commits":7.2,"dds":0.2777777777777778,"last_synced_commit":"094ff8ea25d3640f8204c8c38209a312d010bf19"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjoluc%2Fpytest-reraise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjoluc%2Fpytest-reraise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjoluc%2Fpytest-reraise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bjoluc%2Fpytest-reraise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bjoluc","download_url":"https://codeload.github.com/bjoluc/pytest-reraise/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234180008,"owners_count":18792070,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assert","catch","exception","plugin","pytest","raise","re-raise","threads"],"created_at":"2025-01-16T09:53:43.132Z","updated_at":"2025-01-16T09:53:46.246Z","avatar_url":"https://github.com/bjoluc.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pytest-reraise\n\n[![PyPI](https://img.shields.io/pypi/v/pytest-reraise)](https://pypi.python.org/pypi/pytest-reraise/)\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/bjoluc/pytest-reraise/build.yml)](https://github.com/bjoluc/pytest-reraise/actions)\n[![codecov](https://codecov.io/gh/bjoluc/pytest-reraise/branch/main/graph/badge.svg)](https://codecov.io/gh/bjoluc/pytest-reraise)\n[![PyPI pyversions](https://img.shields.io/pypi/pyversions/pytest-reraise)](https://pypi.python.org/pypi/pytest-reraise/)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/pytest-reraise)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000)](https://github.com/psf/black)\n[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079)](https://github.com/bjoluc/semantic-release-config-poetry)\n\nLet's assume you are writing a pytest test case that includes assertions in another thread, roughly like this:\n\n```python\nfrom threading import Thread\n\ndef test_assert():\n\n    def run():\n        assert False\n\n    Thread(target=run).start()\n```\n\nThis test will pass as the `AssertionError` is not raised in the main thread.\n`pytest-reraise` is here to help you capture the exception and raise it in the main thread:\n\n```sh\npip install pytest-reraise\n```\n\n```python\nfrom threading import Thread\n\ndef test_assert(reraise):\n\n    def run():\n        with reraise:\n            assert False\n\n    Thread(target=run).start()\n```\n\nThe above test will fail as `pytest-reraise` captures the exception and raises it at the end of the test case.\n\n## Advanced Usage and Special Cases\n\n### Wrapping Functions\n\nInstead of using the `reraise` context manager in a function, you can also wrap the entire function with it via the `reraise.wrap()` method.\nHence, the example\n\n```python\ndef run():\n    with reraise:\n        assert False\n\nThread(target=run).start()\n```\n\ncan also be written as\n\n```python\ndef run():\n    assert False\n\nThread(target=reraise.wrap(run)).start()\n```\n\nor even\n\n```python\n@reraise.wrap\ndef run():\n    assert False\n\nThread(target=run).start()\n```\n\n### Manual Re-raising\n\nBy default, the captured exception (if any) is raised at the end of the test case.\nIf you want to raise it before then, call `reraise()` in your test case.\nIf an exception has been raised within a `with reraise` block by then, `reraise()` will raise it right away:\n\n```python\ndef test_assert(reraise):\n\n    def run():\n        with reraise:\n            assert False\n\n    reraise() # This will not raise anything yet\n\n    t = Thread(target=run)\n    t.start()\n    t.join()\n\n    reraise() # This will raise the assertion error\n```\n\nAs seen in the example above, `reraise()` can be called multiple times during a test case. Whenever an exception has been raised in a `with reraise` block since the last call, it will be raised on the next call.\n\n### Multiple Exceptions\n\nWhen the `reraise` context manager is used multiple times in a single test case, only the first-raised exception will be re-raised in the end.\nIn the below example, both threads raise an exception but only one of these exceptions will be re-raised.\n\n```python\ndef test_assert(reraise):\n\n    def run():\n        with reraise:\n            assert False\n\n    for _ in range(2):\n        Thread(target=run).start()\n```\n\n### Catching Exceptions\n\nBy default, the `reraise` context manager does not catch exceptions, so they will not be hidden from the thread in which they are raised.\nIf you want to change this, use `reraise(catch=True)` instead of `reraise`:\n\n```python\ndef test_assert(reraise):\n\n    def run():\n        with reraise(catch=True):\n            assert False\n        print(\"I'm alive!\")\n\n    Thread(target=run).start()\n```\n\nNote that you cannot use `reraise()` (without the `catch` argument) as a context manager, as it is used to raise exceptions.\n\n### Exception Priority\n\nIf `reraise` captures an exception and the main thread raises an exception as well, the exception captured by `reraise` will mask the main thread's exception unless that exception was already re-raised.\nThe objective behind this is that the outcome of the main thread often depends on the work performed in other threads.\nThus, failures in in other threads are likely to cause failures in the main thread, and other threads' exceptions (if any) are of greater importance for the developer than main thread exceptions.\n\nThe example below will report `assert False`, not `assert \"foo\" == \"bar\"`.\n\n```python\ndef test_assert(reraise):\n\n    def run():\n        with reraise:\n            assert False # This will be reported\n\n    t = Thread(target=run)\n    t.start()\n    t.join()\n\n    assert \"foo\" == \"bar\" # This won't\n```\n\n### Accessing and Modifying Exceptions\n\n`reraise` provides an `exception` property to retrieve the exception that was captured, if any.\n`reraise.exception` can also be used to assign an exception if no exception has been captured yet.\nIn addition to that, `reraise.reset()` returns the value of `reraise.exception` and resets it to `None` so that the exception will not be raised anymore.\n\nHere's a quick demonstration test case that passes:\n\n```python\ndef test_assert(reraise):\n\n    def run():\n        with reraise:\n            assert False\n\n    t = Thread(target=run)\n    t.start()\n    t.join()\n\n    # Return the captured exception:\n    assert type(reraise.exception) is AssertionError\n\n    # This won't do anything, since an exception has already been captured:\n    reraise.exception = Exception()\n\n    # Return the exception and set `reraise.exception` to None:\n    assert type(reraise.reset()) is AssertionError\n\n    # `Reraise` will not fail the test case because\n    assert reraise.exception is None\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjoluc%2Fpytest-reraise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbjoluc%2Fpytest-reraise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbjoluc%2Fpytest-reraise/lists"}