RuntimeWarning:在测试中从未等待过coroutine

时间:2018-03-18 17:24:49

标签: python pytest

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

3 个答案:

答案 0 :(得分:0)

这主要是针对用户@may

我如何收到此警告以防止对我吐痰

更改

returned_post = await posts.get_post('0')

returned_post = yield from posts.get_post('0')

并从

中删除了异步
def test_get_post_exists():

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您必须安装pytest-asyncio

pip install pytest-asyncio

您不必将其导入任何地方。只需用@pytest.mark.asyncio装饰异步def测试即可。

@hoefling注释中提到了它,我发布此答案只是为了使其更加可见:

coroutine 'my_coro' was never awaited意味着协程对象从未被放入事件循环中,因此我想您要么缺少将测试放入事件循环的pytest-asyncio。

没有pytest-asyncio,pytest不知道它应该await进行异步测试。它只是将其作为非异步函数运行,不足以实际执行异步函数主体:

$ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
>>> async def foo():
...   raise Excepion('this is never raised')
...
>>> foo()
<coroutine object foo at 0x7f7d857d4cc8>
>>> exit()
sys:1: RuntimeWarning: coroutine 'foo' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback