Skip to content

Commit b45de23

Browse files
simonfagerholmTinche
authored andcommitted
Fixed failing test case, 'test_asyncio_marker_without_loop'.
1 parent 238cced commit b45de23

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

pytest_asyncio/plugin.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,34 @@ def pytest_pyfunc_call(pyfuncitem):
125125
if 'asyncio' in pyfuncitem.keywords:
126126
if getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
127127
pyfuncitem.obj.hypothesis.inner_test = wrap_in_sync(
128-
pyfuncitem.obj.hypothesis.inner_test
128+
pyfuncitem.obj.hypothesis.inner_test,
129+
_loop=pyfuncitem.funcargs['event_loop']
129130
)
130131
else:
131-
pyfuncitem.obj = wrap_in_sync(pyfuncitem.obj)
132+
pyfuncitem.obj = wrap_in_sync(
133+
pyfuncitem.obj,
134+
_loop=pyfuncitem.funcargs['event_loop']
135+
)
132136
yield
133137

134138

135-
def wrap_in_sync(func):
139+
def wrap_in_sync(func, _loop):
136140
"""Return a sync wrapper around an async function executing it in the
137141
current event loop."""
138142

139143
@functools.wraps(func)
140144
def inner(**kwargs):
141145
coro = func(**kwargs)
142146
if coro is not None:
143-
task = asyncio.ensure_future(coro)
144147
try:
145-
asyncio.get_event_loop().run_until_complete(task)
148+
loop = asyncio.get_event_loop()
149+
except RuntimeError as exc:
150+
if 'no current event loop' not in str(exc):
151+
raise
152+
loop = _loop
153+
task = asyncio.ensure_future(coro, loop=loop)
154+
try:
155+
loop.run_until_complete(task)
146156
except BaseException:
147157
# run_until_complete doesn't get the result from exceptions
148158
# that are not subclasses of `Exception`. Consume all

0 commit comments

Comments
 (0)