Skip to content

Commit 54d1d55

Browse files
authored
Merge pull request #64 from smagafurov/master
handle case when no event loop exists (support pytest-aiohttp)
2 parents cf056b1 + 16cc19d commit 54d1d55

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pytest_asyncio/plugin.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ async def setup():
129129
if kw not in request.keywords:
130130
continue
131131
policy = asyncio.get_event_loop_policy()
132-
old_loop = policy.get_event_loop()
132+
try:
133+
old_loop = policy.get_event_loop()
134+
except RuntimeError as exc:
135+
if 'no current event loop' not in str(exc):
136+
raise
137+
old_loop = None
133138
policy.set_event_loop(loop)
134139
fixturedef.addfinalizer(lambda: policy.set_event_loop(old_loop))
135140

tests/test_simple.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@asyncio.coroutine
10-
def async_coro(loop):
10+
def async_coro(loop=None):
1111
"""A very simple coroutine."""
1212
yield from asyncio.sleep(0, loop=loop)
1313
return 'ok'
@@ -143,3 +143,18 @@ def test_asyncio_marker_method(self, event_loop):
143143
"""Test the asyncio pytest marker in a Test class."""
144144
ret = yield from async_coro(event_loop)
145145
assert ret == 'ok'
146+
147+
148+
class TestUnexistingLoop:
149+
@pytest.fixture
150+
def remove_loop(self):
151+
old_loop = asyncio.get_event_loop()
152+
asyncio.set_event_loop(None)
153+
yield
154+
asyncio.set_event_loop(old_loop)
155+
156+
@pytest.mark.asyncio
157+
def test_asyncio_marker_without_loop(self, remove_loop):
158+
"""Test the asyncio pytest marker in a Test class."""
159+
ret = yield from async_coro()
160+
assert ret == 'ok'

0 commit comments

Comments
 (0)