python - Pytest 跳过测试说 "asyncio not installed"

标签 python pytest python-asyncio pytest-asyncio

测试以下代码时

@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",
        }
    }
运行时收到的警告 pytest -vvv在项目环境中:
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)))
我有 pytest-asyncio安装。我通过运行 pytest --trace-config 进行了验证在我项目的虚拟环境中
================== 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

最佳答案

我会留下这个问题,以防其他人遇到这个问题。我最初的问题是我拼错了 asyncio在标记中:pytest.mark.asyncio一旦我确定我需要等待我的回应,所以我不得不将我的测试更改为:

@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",
        }
    }

关于python - Pytest 跳过测试说 "asyncio not installed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55893235/

相关文章:

python - Py.test 反复循环测试

python-3.x - 为什么没有call_at_threadsafe和call_later_threadsafe?

python - 为什么父协程没有被取消?

python - OpenCv 按位运算物理意义

python - 避免缓冲读取 "for line in ..."

python - 使用 Vowpal Wabbit 的命名实体识别似乎可以记住训练数据

python - 如何在 Python 中模拟类内的属性

python3 : do a for loop once more when looped over the whole file

python - PyTest 中单个测试的覆盖标记

python - asyncio.create_task() 做什么?