Skip to content

Commit 3e6d86f

Browse files
committed
Merge pull request #21 from leifurhauks/non_global_loop
Make it possible to avoid using the global loop
2 parents bd0d1f3 + 4c4ded2 commit 3e6d86f

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Diff for: pytest_asyncio/plugin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def pytest_pyfunc_call(pyfuncitem):
4545
testargs = {arg: funcargs[arg]
4646
for arg in pyfuncitem._fixtureinfo.argnames}
4747
event_loop.run_until_complete(
48-
asyncio.async(pyfuncitem.obj(**testargs)))
48+
asyncio.async(pyfuncitem.obj(**testargs), loop=event_loop))
4949
return True
5050

5151

Diff for: tests/test_non_global_loop.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
def setup_module(module):
7+
asyncio.set_event_loop(None)
8+
9+
10+
def teardown_module(module):
11+
# restore the default policy
12+
asyncio.set_event_loop_policy(None)
13+
14+
15+
@pytest.fixture
16+
def event_loop(request):
17+
loop = asyncio.new_event_loop()
18+
request.addfinalizer(loop.close)
19+
return loop
20+
21+
22+
@asyncio.coroutine
23+
def example_coroutine():
24+
return 42
25+
26+
27+
@pytest.mark.asyncio
28+
def test_asyncio_marker_with_non_global_loop(event_loop):
29+
with pytest.raises(RuntimeError):
30+
_ = asyncio.get_event_loop()
31+
32+
result = yield from example_coroutine()
33+

0 commit comments

Comments
 (0)