Mocking async call in python 3.5

The solution was actually quite simple: I just needed to convert __call__ method of mock into coroutine: class AsyncMock(MagicMock): async def __call__(self, *args, **kwargs): return super(AsyncMock, self).__call__(*args, **kwargs) This works perfectly, when mock is called, code receives native coroutine Example usage: @mock.patch(‘my.path.asyncio.sleep’, new_callable=AsyncMock) def test_stuff(sleep): # code

Python Mocking a function from an imported module

When you are using the patch decorator from the unittest.mock package you are patching it in the namespace that is under test (in this case app.mocking.get_user_name), not the namespace the function is imported from (in this case app.my_module.get_user_name). To do what you describe with @patch try something like the below: from mock import patch from … Read more