[英]Testing python asyncio coroutine with pytest-asyncio plugin raises TypeError
I am trying to test the following method with the pytest asyncio plugin: 我正在尝试使用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
And I've written the following test: 而且我编写了以下测试:
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'
This test raises the following error: 此测试引发以下错误:
item = await self.item_collection.find_one({'item_id': item_id})
TypeError: object dict can't be used in 'await' expression
If I remove the await in item = await self.item_collection.find_one({'item_id': item_id})
the test passes, but how can I go about testing this method as it is? 如果我删除
item = await self.item_collection.find_one({'item_id': item_id})
中的await,则测试通过,但是如何继续测试此方法呢?
As mentioned in the comments, mongomock does not play well with asyncio. 如评论中所述,mongomock在asyncio中不能很好地发挥作用。 I created a package that should work with mongodb motor for asyncio calls: https://github.com/xzased/pytest-async-mongodb
我创建了一个应与mongodb motor一起用于异步调用的软件包: https : //github.com/xzased/pytest-async-mongodb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.