Skip to content
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

"The test […] is not an async function" when asyncio marker is added via pytest_collection_modifyitems() #810

Open
mgorny opened this issue Apr 2, 2024 · 1 comment
Labels

Comments

@mgorny
Copy link
Contributor

mgorny commented Apr 2, 2024

This one I've noticed while testing IPython. The simplest reproducer is:

conftest.py:

import inspect


def pytest_collection_modifyitems(items):
    for item in items:
        if inspect.iscoroutinefunction(item.obj):
            item.add_marker("asyncio")

test_foo.py:

async def test_foo():
    pass

With pytest-asyncio 0.23.6, I'm getting:

========================================================= test session starts =========================================================
platform linux -- Python 3.11.8, pytest-8.1.1, pluggy-1.4.0
rootdir: /tmp/repro
plugins: asyncio-0.23.6
asyncio: mode=Mode.STRICT
collected 1 item                                                                                                                      

test_foo.py s                                                                                                                   [100%]

========================================================== warnings summary ===========================================================
test_foo.py::test_foo
  test_foo.py:5: PytestWarning: The test <Function test_foo> is marked with '@pytest.mark.asyncio' but it is not an async function. Please remove the asyncio mark. If the test is not marked explicitly, check for global marks applied via 'pytestmark'.
    async def test_foo():

test_foo.py::test_foo
  /tmp/repro/.venv/lib/python3.11/site-packages/_pytest/python.py:184: PytestUnhandledCoroutineWarning: async def functions are not natively supported and have been skipped.
  You need to install a suitable plugin for your async framework, for example:
    - anyio
    - pytest-asyncio
    - pytest-tornasync
    - pytest-trio
    - pytest-twisted
    warnings.warn(PytestUnhandledCoroutineWarning(msg.format(nodeid)))

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=================================================== 1 skipped, 2 warnings in 0.01s ====================================================

With pytest-asyncio-0.21.1, the test is correctly run as marked.

@mgorny
Copy link
Contributor Author

mgorny commented Apr 16, 2024

Also affects PyZMQ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants