diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 4c690466..7941e2a9 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -45,7 +45,7 @@ def pytest_pyfunc_call(pyfuncitem): testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames} event_loop.run_until_complete( - asyncio.async(pyfuncitem.obj(**testargs))) + asyncio.async(pyfuncitem.obj(**testargs), loop=event_loop)) return True diff --git a/tests/test_non_global_loop.py b/tests/test_non_global_loop.py new file mode 100644 index 00000000..e8098c13 --- /dev/null +++ b/tests/test_non_global_loop.py @@ -0,0 +1,33 @@ +import asyncio + +import pytest + + +def setup_module(module): + asyncio.set_event_loop(None) + + +def teardown_module(module): + # restore the default policy + asyncio.set_event_loop_policy(None) + + +@pytest.fixture +def event_loop(request): + loop = asyncio.new_event_loop() + request.addfinalizer(loop.close) + return loop + + +@asyncio.coroutine +def example_coroutine(): + return 42 + + +@pytest.mark.asyncio +def test_asyncio_marker_with_non_global_loop(event_loop): + with pytest.raises(RuntimeError): + _ = asyncio.get_event_loop() + + result = yield from example_coroutine() +