在测试以下代码时
@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:协程函数本身不受支持,已被跳过。
您需要为异步框架安装合适的插件,例如:
pytest-asyncio
pytest-trio
pytest-tornasync
warnings.warn(PytestWarning(msg.format(pyfuncitem.nodeid)))
我已经安装了pytest-asyncio。我通过在项目的虚拟环境中运行pytest --trace-config
进行了验证
===================测试会话开始=====================
平台win32-Python 3.7.1,pytest-4.4.1,py-1.8.0,pluggy-0.9.0
使用:pytest-4.4.1 pylib-1.8.0
...
setuptools
已注册插件:
pytest-randomly-3.0.0,位于\ lib \ site-packages \ pytest_randomly.py
pytest-mock-1.10.2,位于\ lib \ site-packages \ pytest_mock.py
pytest-asyncio-0.10.0,位于\ lib \ site-packages \ pytest_asyncio \ plugin.py
活动插件:
pytest_mock:\ lib \ site-packages \ pytest_mock.py
asyncio:\ lib \ site-packages \ pytest_asyncio \ plugin.py
...
插件:random-3.0.0,mock-1.10.2,asyncio-0.10.0
答案 0 :(得分: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",
}
}