gpt4 book ai didi

python - 运行时警告: coroutine was never awaited in tests

转载 作者:行者123 更新时间:2023-12-01 22:46:07 24 4
gpt4 key购买 nike

我正在尝试使用 pytest 测试异步 Python 函数。我的测试通过了,但出现了警告,而实际上它应该失败。

这是我的测试:

import asynctest
import pytest

from Foo.bar import posts

@pytest.mark.asyncio
async def test_get_post_exists():
returned_post = await posts.get_post('0')

assert returned_post.id == 0
assert returned_post.text == 'Text for the post body.'
assert True == False

它通过以下消息:

================================== test session starts ===================================
platform darwin -- Python 3.6.4, pytest-3.4.2, py-1.5.2, pluggy-0.6.0
rootdir: /path/path/path/path, inifile:
collected 1 item

tests/unit/route_logic/test_post_helpers.py . [100%]

==================================== warnings summary ====================================
tests/unit/route_logic/test_post_helpers.py::test_get_post_exists
/usr/local/lib/python3.6/site-packages/_pytest/python.py:155: RuntimeWarning: coroutine 'test_get_post_exists' was never awaited
testfunction(**testargs)

-- Docs: http://doc.pytest.org/en/latest/warnings.html
========================== 1 passed, 1 warnings in 0.10 seconds ==========================

最佳答案

这是使用pytestpytest-asyncio的工作演示:

Foo/bar/posts.py:

from collections import namedtuple

Post = namedtuple('Post', 'id text')


async def get_post(id: str):
return Post(id=int(id), text='Text for the post body.')

test_post_helpers.py:

import pytest

from Foo.bar import posts


@pytest.mark.asyncio
async def test_get_post_exists():
returned_post = await posts.get_post('0')
assert returned_post.id == 0
assert returned_post.text == 'Text for the post body.'
assert True == False

单元测试结果:

========================================================================================================================================================================= test session starts ==========================================================================================================================================================================
platform darwin -- Python 3.7.5, pytest-5.3.1, py-1.8.0, pluggy-0.13.1
rootdir: /Users/ldu020/workspace/github.com/mrdulin/python-codelab
plugins: asyncio-0.10.0
collected 1 item

src/stackoverflow/49350821/test_post_helpers.py F [100%]

=============================================================================================================================================================================== FAILURES ===============================================================================================================================================================================
_________________________________________________________________________________________________________________________________________________________________________ test_get_post_exists _________________________________________________________________________________________________________________________________________________________________________

@pytest.mark.asyncio
async def test_get_post_exists():
returned_post = await posts.get_post('0')
assert returned_post.id == 0
assert returned_post.text == 'Text for the post body.'
> assert True == False
E assert True == False

src/stackoverflow/49350821/test_post_helpers.py:11: AssertionError
========================================================================================================================================================================== 1 failed in 0.15s ===========================================================================================================================================================================

assert True == False 导致断言如您所愿失败。

源代码:https://github.com/mrdulin/python-codelab/tree/master/src/stackoverflow/49350821

关于python - 运行时警告: coroutine was never awaited in tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49350821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com