测试蟒蛇ASYNCIO协程与pytest-ASYNCIO插件引发类型错误
问题描述:
我试图测试与pytest ASYNCIO插件下面的方法:测试蟒蛇ASYNCIO协程与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
如果我删除的await在item = await self.item_collection.find_one({'item_id': item_id})
的检验p驴,但我该怎么去测试这个方法,因为它是?
答
正如在评论中提到的那样,mongomock与asyncio不兼容。我创建了一个应该与mongodb电机一起使用以进行asyncio调用的软件包:https://github.com/xzased/pytest-async-mongodb
您是在嘲笑'find_one'吗? – dirn
嗨@dirn,我使用python-mongodb插件和json fixtures,后者又使用MongoMock。嘲笑的蒙戈收藏品按预期工作,对不起,我把它留下了。 –
它看起来不像MongoMock理解asyncio。它定义了一个具有常规方法的集合,而不是协程。你在用什么驱动程序?发动机?你可能需要寻找一个可以嘲笑或自己处理它的库。 – dirn