簡體   English   中英

使用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 motor一起用於異步調用的軟件包: https : //github.com/xzased/pytest-async-mongodb

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM