-
Notifications
You must be signed in to change notification settings - Fork 172
Closed
Description
Using module-global marker pytestmark = pytest.mark.asyncio is an elegant way to achieve #61. However, it currently fails if the module has async def and classic def tests intermixed:
import pytest
import asyncio
pytestmark = pytest.mark.asyncio
def test_classic():
assert bool(1) == True
async def test_async():
await asyncio.sleep(0)
assert bool(0) == False============================= test session starts =============================
platform win32 -- Python 3.6.1, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
rootdir: C:\temp, inifile:
plugins: asyncio-0.10.0
collected 2 items
asyncio-test.py F. [100%]
================================== FAILURES ===================================
________________________________ test_classic _________________________________
pyfuncitem = <Function test_classic>
@pytest.mark.tryfirst
def pytest_pyfunc_call(pyfuncitem):
"""
Run asyncio marked test functions in an event loop instead of a normal
function call.
"""
for marker_name, fixture_name in _markers_2_fixtures.items():
if marker_name in pyfuncitem.keywords \
and not getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
event_loop = pyfuncitem.funcargs[fixture_name]
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg]
for arg in pyfuncitem._fixtureinfo.argnames}
event_loop.run_until_complete(
asyncio.ensure_future(
> pyfuncitem.obj(**testargs), loop=event_loop))
C:\ProgramData\Anaconda3\lib\site-packages\pytest_asyncio\plugin.py:158:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
coro_or_future = None
def ensure_future(coro_or_future, *, loop=None):
"""Wrap a coroutine or an awaitable in a future.
If the argument is a Future, it is returned directly.
"""
if futures.isfuture(coro_or_future):
if loop is not None and loop is not coro_or_future._loop:
raise ValueError('loop argument must agree with Future')
return coro_or_future
elif coroutines.iscoroutine(coro_or_future):
if loop is None:
loop = events.get_event_loop()
task = loop.create_task(coro_or_future)
if task._source_traceback:
del task._source_traceback[-1]
return task
elif compat.PY35 and inspect.isawaitable(coro_or_future):
return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
else:
> raise TypeError('A Future, a coroutine or an awaitable is required')
E TypeError: A Future, a coroutine or an awaitable is required
C:\ProgramData\Anaconda3\lib\asyncio\tasks.py:520: TypeError
===================== 1 failed, 1 passed in 0.29 seconds ======================It is proposed that pytest.mark.asyncio is compatible with classic tests also (being a no-op there).
Metadata
Metadata
Assignees
Labels
No labels