Skip to content

Marker pytest.mark.asyncio should be compatible with classic functions also #109

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

Closed
aparamon opened this issue Feb 2, 2019 · 2 comments
Closed

Comments

@aparamon
Copy link

aparamon commented Feb 2, 2019

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).

@uriva
Copy link

uriva commented Dec 9, 2019

Why is this a flag rather than the default?

@ghost
Copy link

ghost commented May 12, 2020

Can this be closed? Seems the fix for this is already on PyPi unless I am mistaken.

pip install pytest pytest-asyncio
image

@asvetlov asvetlov closed this as completed Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants