Skip to content

Commit

Permalink
Handle BaseExceptions from loop.run_until_complete (#126)
Browse files Browse the repository at this point in the history
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 <github@thequod.de>
Co-Authored-By: Andrew Svetlov <andrew.svetlov@gmail.com>
  • Loading branch information
3 people committed Jul 16, 2019
1 parent a9e2213 commit 86cd9a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pytest_asyncio/plugin.py
Expand Up @@ -140,9 +140,16 @@ def wrap_in_sync(func):
def inner(**kwargs):
coro = func(**kwargs)
if coro is not None:
future = asyncio.ensure_future(coro)
asyncio.get_event_loop().run_until_complete(future)

task = asyncio.ensure_future(coro)
try:
asyncio.get_event_loop().run_until_complete(task)
except BaseException:
# run_until_complete doesn't get the result from exceptions
# that are not subclasses of `Exception`. Consume all
# exceptions to prevent asyncio's warning from logging.
if task.done() and not task.cancelled():
task.exception()
raise
return inner


Expand Down
5 changes: 5 additions & 0 deletions tests/test_simple.py
Expand Up @@ -134,3 +134,8 @@ async def test_asyncio_marker_without_loop(self, remove_loop):
"""Test the asyncio pytest marker in a Test class."""
ret = await async_coro()
assert ret == 'ok'


@pytest.mark.asyncio
async def test_no_warning_on_skip():
pytest.skip("Test a skip error inside asyncio")

0 comments on commit 86cd9a6

Please sign in to comment.