From 0e1132c92671dc70d60c41a0b7e94d2e7a124268 Mon Sep 17 00:00:00 2001 From: Sergey Magafurov Date: Mon, 24 Jul 2017 15:36:19 +0500 Subject: [PATCH 1/2] handle case when no event loop exists (support pytest-aiohttp) --- pytest_asyncio/plugin.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 309bdf76..a99e9ec3 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -129,7 +129,12 @@ async def setup(): if kw not in request.keywords: continue policy = asyncio.get_event_loop_policy() - old_loop = policy.get_event_loop() + try: + old_loop = policy.get_event_loop() + except RuntimeError as exc: + if 'no current event loop' not in str(exc): + raise + old_loop = None policy.set_event_loop(loop) fixturedef.addfinalizer(lambda: policy.set_event_loop(old_loop)) From 16cc19d03d1922078372dc9f02ab1215f03fb1ca Mon Sep 17 00:00:00 2001 From: Sergey Magafurov Date: Mon, 11 Sep 2017 13:47:09 +0500 Subject: [PATCH 2/2] handle case when no event loop exists (add test) --- tests/test_simple.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_simple.py b/tests/test_simple.py index 35995d97..152d88f1 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -7,7 +7,7 @@ @asyncio.coroutine -def async_coro(loop): +def async_coro(loop=None): """A very simple coroutine.""" yield from asyncio.sleep(0, loop=loop) return 'ok' @@ -143,3 +143,18 @@ def test_asyncio_marker_method(self, event_loop): """Test the asyncio pytest marker in a Test class.""" ret = yield from async_coro(event_loop) assert ret == 'ok' + + +class TestUnexistingLoop: + @pytest.fixture + def remove_loop(self): + old_loop = asyncio.get_event_loop() + asyncio.set_event_loop(None) + yield + asyncio.set_event_loop(old_loop) + + @pytest.mark.asyncio + def test_asyncio_marker_without_loop(self, remove_loop): + """Test the asyncio pytest marker in a Test class.""" + ret = yield from async_coro() + assert ret == 'ok'