Skip to content

asyncio marker works if no event loop is set #65

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

Closed
wants to merge 1 commit into from
Closed
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: 5 additions & 2 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 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,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'