Pytest skips test saying "asyncio not installed"
Asked Answered
R

2

31

When testing the following code

@pytest.mark.asynico
async def test_handle_DATA(mocker):
    handle_mock = mocker.MagicMock()
    envelope_mock = mocker.MagicMock(mail_from="Test@From", rcpt_tos=["Test@To"], content=b"TestContent")

    result = SendToDictHandler.handle_DATA(handle_mock, "TestServer", "TestSession", envelope_mock)

    assert result == "250 Message accepted for delivery"
    assert email_core.testing_emails_dict == {
        "Test@To": {
            "from": "Test@From",
            "to": ["Test@To"],
            "msg": "TestContent",
        }
    }

The warning I get when running pytest -vvv in the projects environment:

PytestWarning: Coroutine functions are not natively supported and have been skipped.
You need to install a suitable plugin for your async framework, for example:
 - pytest-asyncio
 - pytest-trio
 - pytest-tornasync
warnings.warn(PytestWarning(msg.format(pyfuncitem.nodeid)))

I do have pytest-asyncio installed. I verified by running pytest --trace-config in my project's virtual environment

================== test session starts ======================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.9.0
using: pytest-4.4.1 pylib-1.8.0
...
setuptools 
registered plugins:
 - pytest-randomly-3.0.0 at \lib\site-packages\pytest_randomly.py
 - pytest-mock-1.10.2 at \lib\site-packages\pytest_mock.py
 - pytest-asyncio-0.10.0 at \lib\site-packages\pytest_asyncio\plugin.py

active plugins:
 - pytest_mock         : \lib\site-packages\pytest_mock.py
 - asyncio             : \lib\site-packages\pytest_asyncio\plugin.py
...

plugins: randomly-3.0.0, mock-1.10.2, asyncio-0.10.0
Refractor answered 28/4, 2019 at 18:17 Comment(0)
R
36

I'll leave this issue up incase someone else runs into this problem. My initial problem was I misspelled asyncio in the marker: pytest.mark.asyncio

Once I fixed that I needed to await my response so I had to change my test to this:

@staticmethod
@pytest.mark.asyncio
async def test_handle_DATA(mocker):
    handle_mock = mocker.MagicMock()
    envelope_mock = mocker.MagicMock(mail_from="Test@From", rcpt_tos=["Test@To"], content=b"TestContent")

    assert "250 Message accepted for delivery" == await SendToDictHandler.handle_DATA(
        handle_mock, "TestServer", "TestSession", envelope_mock
    )
    assert email_core.testing_emails_dict == {
        "Test@To": {
            "from": "Test@From",
            "to": ["Test@To"],
            "msg": "TestContent",
        }
    }
Refractor answered 28/4, 2019 at 20:6 Comment(1)
I had the same error, but my mistake was forgetting the mark entirely. When I read about your misspelling, it reminded me.Fruitful
I
4

In terms of python's very likely asynchronous future, setting the pytest-asyncio mode to auto makes sense :)

E.g. in pyproject.toml

[tool.pytest.ini_options]
asyncio_mode = "auto"

Innermost answered 28/12, 2023 at 14:46 Comment(1)
Besides installing pytest-asyncio, going from (default) strict to asyncio_mode = auto solved it for me.Multifid

© 2022 - 2025 — McMap. All rights reserved.