Skip to content

Commit 86cd9a6

Browse files
jcristblueyedasvetlov
committed
Handle BaseExceptions from loop.run_until_complete (#126)
Asyncio doesn't get the results of tasks that fail with exceptions that don't subclass `Exception` (like pytest's `Skipped`) in `run_until_complete`. This leads to asyncio logging noisily warnings when pytest's skip functionality is mixed with this plugin. This fixes this error, and adds a test. Fixes #123. Co-Authored-By: Daniel Hahler <[email protected]> Co-Authored-By: Andrew Svetlov <[email protected]>
1 parent a9e2213 commit 86cd9a6

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pytest_asyncio/plugin.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,16 @@ def wrap_in_sync(func):
140140
def inner(**kwargs):
141141
coro = func(**kwargs)
142142
if coro is not None:
143-
future = asyncio.ensure_future(coro)
144-
asyncio.get_event_loop().run_until_complete(future)
145-
143+
task = asyncio.ensure_future(coro)
144+
try:
145+
asyncio.get_event_loop().run_until_complete(task)
146+
except BaseException:
147+
# run_until_complete doesn't get the result from exceptions
148+
# that are not subclasses of `Exception`. Consume all
149+
# exceptions to prevent asyncio's warning from logging.
150+
if task.done() and not task.cancelled():
151+
task.exception()
152+
raise
146153
return inner
147154

148155

tests/test_simple.py

+5
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,8 @@ async def test_asyncio_marker_without_loop(self, remove_loop):
134134
"""Test the asyncio pytest marker in a Test class."""
135135
ret = await async_coro()
136136
assert ret == 'ok'
137+
138+
139+
@pytest.mark.asyncio
140+
async def test_no_warning_on_skip():
141+
pytest.skip("Test a skip error inside asyncio")

0 commit comments

Comments
 (0)