运行时警告:在测试中从未等待协同程序

2024-10-02 18:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用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

它传递以下消息:

^{pr2}$

Tags: 函数fromimportasyncio警告getfoopytest
1条回答
网友
1楼 · 发布于 2024-10-02 18:19:29

下面是使用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

^{pr2}$

单元测试结果:

========================================================================================================================================================================= 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

相关问题 更多 >