Skip to content

handle case when no event loop exists (support pytest-aiohttp) #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
17 changes: 16 additions & 1 deletion tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'