[英]Testing python asyncio coroutine with pytest-asyncio plugin raises TypeError
我正在尝试使用pytest asyncio插件测试以下方法:
class Controller(object):
async def get_item(self, item_id):
item = await self.item_collection.find_one({'item_id': item_id})
return item
而且我编写了以下测试:
class TestController(object):
@pytest.mark.asyncio
async def test_get_item(self):
controller = Controller()
item = await controller.get_item('item-1')
assert item.get('item_id') == 'item-1'
此测试引发以下错误:
item = await self.item_collection.find_one({'item_id': item_id})
TypeError: object dict can't be used in 'await' expression
如果我删除item = await self.item_collection.find_one({'item_id': item_id})
中的await,则测试通过,但是如何继续测试此方法呢?
如评论中所述,mongomock在asyncio中不能很好地发挥作用。 我创建了一个应与mongodb motor一起用于异步调用的软件包: https : //github.com/xzased/pytest-async-mongodb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.