Skip to content

Commit

Permalink
Test exceptions in hookwrapper post stage (pytest-dev#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnikulin committed Feb 8, 2022
1 parent 172ca9f commit 7828947
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions testing/test_multicall.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,79 @@ def m2():
with pytest.raises(exc):
MC([m2, m1], {})
assert out == ["m1 init", "m1 finish"]


def test_unwind_inner_wrapper_teardown_exc() -> None:
out = []

@hookimpl(hookwrapper=True)
def m1():
out.append("m1 init")
try:
outcome = yield 1
out.append("m1 teardown")
outcome.get_result()
out.append("m1 unreachable")
finally:
out.append("m1 cleanup")

@hookimpl(hookwrapper=True)
def m2():
out.append("m2 init")
yield 2
out.append("m2 raise")
raise ValueError()

with pytest.raises(ValueError):
try:
MC([m2, m1], {})
finally:
out.append("finally")

assert out == [
"m1 init",
"m2 init",
"m2 raise",
"m1 teardown",
"m1 cleanup",
"finally",
]


def test_suppress_inner_wrapper_teardown_exc() -> None:
out = []

@hookimpl(hookwrapper=True)
def m1():
out.append("m1 init")
outcome = yield 1
outcome.get_result()
out.append("m1 finish")

@hookimpl(hookwrapper=True)
def m2():
out.append("m2 init")
try:
outcome = yield 2
outcome.get_result()
out.append("m2 unreachable")
except ValueError:
outcome.force_result(22)
out.append("m2 suppress")

@hookimpl(hookwrapper=True)
def m3():
out.append("m3 init")
yield 3
out.append("m3 raise")
raise ValueError()

assert 22 == MC([m3, m2, m1], {})
assert out == [
"m1 init",
"m2 init",
"m3 init",
"m3 raise",
"m2 suppress",
"m1 finish",
]

0 comments on commit 7828947

Please sign in to comment.