Mocking async call in python 3.5

后端 未结 7 1552
花落未央
花落未央 2020-12-04 23:38

How do I mock async call from one native coroutine to other one using unittest.mock.patch?

I currently have quite an awkward solution:

c         


        
相关标签:
7条回答
  • 2020-12-05 00:31

    You can set the return_value of an async method like so:

    mock = unittest.mock.MagicMock()
    mock.your_async_method.return_value = task_from_result(your_return_value)
    
    async def task_from_result(result):
        return result
    

    The caller will have to do await your_async_method(..) just like as if the method wasn't mocked.

    0 讨论(0)
提交回复
热议问题