From 5d124a2dab344cefbeaba26d3b714397e316cb20 Mon Sep 17 00:00:00 2001 From: Jim Crist Date: Tue, 2 Jul 2019 17:07:36 -0500 Subject: [PATCH 1/3] No warning on skip in asyncio 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 warning when pytest's skip functionality is mixed with this plugin. This fixes this error, and adds a test. --- pytest_asyncio/plugin.py | 13 ++++++++++--- tests/test_simple.py | 5 +++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 24450bf7..17e3a85d 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -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(): + task.exception() + raise return inner diff --git a/tests/test_simple.py b/tests/test_simple.py index f9e3992f..bf3a18d7 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -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("Need a skip error inside asyncio") From 0e6b0dd053879ea058551cef43ac4697a76267c3 Mon Sep 17 00:00:00 2001 From: Jim Crist Date: Tue, 2 Jul 2019 18:07:44 -0500 Subject: [PATCH 2/3] Update tests/test_simple.py Co-Authored-By: Daniel Hahler --- tests/test_simple.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_simple.py b/tests/test_simple.py index bf3a18d7..1627139d 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -138,4 +138,4 @@ async def test_asyncio_marker_without_loop(self, remove_loop): @pytest.mark.asyncio async def test_no_warning_on_skip(): - pytest.skip("Need a skip error inside asyncio") + pytest.skip("Test a skip error inside asyncio") From dbc7b8a7ac03cc34ef35f98eb7e79deae4cb7227 Mon Sep 17 00:00:00 2001 From: Jim Crist Date: Tue, 16 Jul 2019 12:25:21 -0500 Subject: [PATCH 3/3] fixup Co-Authored-By: Andrew Svetlov --- pytest_asyncio/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 17e3a85d..39b9c698 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -147,7 +147,7 @@ def inner(**kwargs): # 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(): + if task.done() and not task.cancelled(): task.exception() raise return inner