Skip to content

Fix event_loop is run after other fixtures #156

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 3 commits into from
May 3, 2020
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
26 changes: 19 additions & 7 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,34 @@ def pytest_pyfunc_call(pyfuncitem):
if 'asyncio' in pyfuncitem.keywords:
if getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
pyfuncitem.obj.hypothesis.inner_test = wrap_in_sync(
pyfuncitem.obj.hypothesis.inner_test
pyfuncitem.obj.hypothesis.inner_test,
_loop=pyfuncitem.funcargs['event_loop']
)
else:
pyfuncitem.obj = wrap_in_sync(pyfuncitem.obj)
pyfuncitem.obj = wrap_in_sync(
pyfuncitem.obj,
_loop=pyfuncitem.funcargs['event_loop']
)
yield


def wrap_in_sync(func):
def wrap_in_sync(func, _loop):
"""Return a sync wrapper around an async function executing it in the
current event loop."""

@functools.wraps(func)
def inner(**kwargs):
coro = func(**kwargs)
if coro is not None:
task = asyncio.ensure_future(coro)
try:
asyncio.get_event_loop().run_until_complete(task)
loop = asyncio.get_event_loop()
except RuntimeError as exc:
if 'no current event loop' not in str(exc):
raise
loop = _loop
task = asyncio.ensure_future(coro, loop=loop)
try:
loop.run_until_complete(task)
except BaseException:
# run_until_complete doesn't get the result from exceptions
# that are not subclasses of `Exception`. Consume all
Expand All @@ -154,9 +164,11 @@ def inner(**kwargs):


def pytest_runtest_setup(item):
if 'asyncio' in item.keywords and 'event_loop' not in item.fixturenames:
if 'asyncio' in item.keywords:
# inject an event loop fixture for all async tests
item.fixturenames.append('event_loop')
if 'event_loop' in item.fixturenames:
item.fixturenames.remove('event_loop')
item.fixturenames.insert(0, 'event_loop')
if item.get_closest_marker("asyncio") is not None \
and not getattr(item.obj, 'hypothesis', False) \
and getattr(item.obj, 'is_hypothesis_test', False):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ async def test_asyncio_marker_without_loop(self, remove_loop):
assert ret == 'ok'


class TestEventLoopStartedBeforeFixtures:
@pytest.fixture
async def loop(self):
return asyncio.get_event_loop()

@staticmethod
def foo():
return 1

@pytest.mark.asyncio
async def test_no_event_loop(self, loop):
assert await loop.run_in_executor(None, self.foo) == 1

@pytest.mark.asyncio
async def test_event_loop_after_fixture(self, loop, event_loop):
assert await loop.run_in_executor(None, self.foo) == 1

@pytest.mark.asyncio
async def test_event_loop_before_fixture(self, event_loop, loop):
assert await loop.run_in_executor(None, self.foo) == 1



@pytest.mark.asyncio
async def test_no_warning_on_skip():
pytest.skip("Test a skip error inside asyncio")