Skip to content

Commit fb245f6

Browse files
Support strict_exception_groups with timeouts
1 parent 34ebecb commit fb245f6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pytest_trio/plugin.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
from trio.abc import Clock, Instrument
1313
from trio.testing import MockClock
1414
from _pytest.outcomes import Skipped, XFailed
15+
1516
# pytest_timeout_set_timer needs to be imported here for pluggy
16-
from .timeout import set_timeout, pytest_timeout_set_timer as pytest_timeout_set_timer
17+
from .timeout import (
18+
set_timeout,
19+
TimeoutTriggeredException,
20+
pytest_timeout_set_timer as pytest_timeout_set_timer,
21+
)
1722

1823
if sys.version_info[:2] < (3, 11):
1924
from exceptiongroup import BaseExceptionGroup
@@ -362,6 +367,8 @@ def wrapper(**kwargs):
362367
ex = queue.pop()
363368
if isinstance(ex, BaseExceptionGroup):
364369
queue.extend(ex.exceptions)
370+
elif isinstance(ex, TimeoutTriggeredException):
371+
pytest.fail(str(ex), pytrace=False)
365372
else:
366373
leaves.append(ex)
367374
if len(leaves) == 1:
@@ -372,6 +379,8 @@ def wrapper(**kwargs):
372379
# Since our leaf exceptions don't consist of exactly one 'magic'
373380
# skipped or xfailed exception, re-raise the whole group.
374381
raise
382+
except TimeoutTriggeredException as ex:
383+
pytest.fail(str(ex), pytrace=False)
375384

376385
return wrapper
377386

pytest_trio/timeout.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def pytest_timeout_set_timer(
5050
# No need for pytest_timeout_cancel_timer as we detect that the test loop has exited
5151

5252

53+
class TimeoutTriggeredException(Exception):
54+
pass
55+
56+
5357
def trio_timeout_thread():
5458
async def run_timeouts():
5559
async with trio.open_nursery() as nursery:
@@ -89,7 +93,7 @@ async def execute_timeout() -> None:
8993
stack = "\n".join(format_recursive_nursery_stack(nursery) + ["Timeout reached"])
9094

9195
async def report():
92-
pytest.fail(stack, pytrace=False)
96+
raise TimeoutTriggeredException(stack)
9397

9498
nursery.start_soon(report)
9599

0 commit comments

Comments
 (0)