Skip to content

Match only_rerun against wrapped exception causes - #364

Open
teddytennant wants to merge 1 commit into
pytest-dev:masterfrom
teddytennant:fix/pytest-rerunfailures
Open

Match only_rerun against wrapped exception causes#364
teddytennant wants to merge 1 commit into
pytest-dev:masterfrom
teddytennant:fix/pytest-rerunfailures

Conversation

@teddytennant

Copy link
Copy Markdown
Contributor

Fixes #353.

--only-rerun / only_rerun (and --rerun-except) match the outermost exception only. A transient error wrapped with raise ... from is not retried, even when the inner type is exactly what the filter is for.

def test_wrapped():
    try:
        raise MemoryError("out of memory")
    except MemoryError as error:
        raise RuntimeError("something failed") from error
$ pytest test_wrapped.py --reruns 2 --only-rerun MemoryError
1 failed            # not rerun

The same test without the wrapper reruns as expected. _try_match_error built the match string from excinfo.type / excinfo.value and never walked __cause__ or __context__.

The matcher now walks that chain (skipping a context suppressed by raise ... from None) and treats a hit on any linked exception as a match. Existing outer-exception matches are unchanged.

Regression tests cover a __cause__ wrap, an implicit __context__ wrap, the exception-class form of only_rerun, rerun_except on a wrapped error, a suppressed context, and a non-matching control.

$ pytest tests/test_pytest_rerunfailures.py -k wrapped
7 passed
$ pytest tests/
201 passed, 7 skipped

Wrapped errors such as raise RuntimeError(...) from MemoryError(...)
were matched on the outer type and message only, so --only-rerun
MemoryError did not retry them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

only_rerun does not match exceptions wrapped by another exception (__cause__ / __context__ not inspected)

1 participant