From 0097feadf753ee0946ae9dcac17e2a9669484dd8 Mon Sep 17 00:00:00 2001 From: manuelmiranda Date: Fri, 8 Sep 2017 17:30:15 +0200 Subject: [PATCH] asyncio marker works if no event loop is set --- pytest_asyncio/plugin.py | 7 +++++-- tests/test_simple.py | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 309bdf76..cd109021 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -129,9 +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() + fixturedef.addfinalizer(lambda: policy.set_event_loop(old_loop)) + except RuntimeError: + pass policy.set_event_loop(loop) - fixturedef.addfinalizer(lambda: policy.set_event_loop(old_loop)) @pytest.mark.tryfirst diff --git a/tests/test_simple.py b/tests/test_simple.py index 35995d97..d1ed72cd 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,19 @@ 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'