使用 pytest-asyncio 插件测试 python asyncio 协程引发 TypeError
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 电机一起用于异步调用:https://github.com/xzased/pytest-async-mongodb
我正在尝试使用 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 电机一起用于异步调用:https://github.com/xzased/pytest-async-mongodb