简体   繁体   中英

Testing python asyncio coroutine with pytest-asyncio plugin raises TypeError

I am trying to test the following method with the pytest asyncio plugin:

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?

As mentioned in the comments, mongomock does not play well with asyncio. I created a package that should work with mongodb motor for asyncio calls: https://github.com/xzased/pytest-async-mongodb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM