Python - object MagicMock can't be used in 'await' expression - python

When I was trying to mock an async function in unittest with MagicMock, I got this exception:
TypeError: object MagicMock can't be used in 'await' expression
With sample code like:
# source code
class Service:
async def compute(self, x):
return x
class App:
def __init__(self):
self.service = Service()
async def handle(self, x):
return await self.service.compute(x)
# test code
import asyncio
import unittest
from unittest.mock import patch
class TestApp(unittest.TestCase):
#patch('__main__.Service')
def test_handle(self, mock):
loop = asyncio.get_event_loop()
app = App()
res = loop.run_until_complete(app.handle('foo'))
app.service.compute.assert_called_with("foo")
if __name__ == '__main__':
unittest.main()
How should I fix it with built-in python3 libraries?

In python 3.8+ you can make use of the AsyncMock
async def test_that_mock_can_be_awaited():
mock = AsyncMock()
mock.x.return_value = 123
result = await mock.x()
assert result == 123
The class AsyncMock object will behave so the object is recognized as an async function, and the result of a call is an awaitable.
>>> mock = AsyncMock()
>>> asyncio.iscoroutinefunction(mock)
True
>>> inspect.isawaitable(mock())
True

I ended up with this hack.
# monkey patch MagicMock
async def async_magic():
pass
MagicMock.__await__ = lambda x: async_magic().__await__()
It only works for MagicMock, not other pre-defined return_value

You can get mocks to return objects that can be awaited by using a Future. The following is a pytest test case, but something similar should be possible with unittest.
async def test_that_mock_can_be_awaited():
mock = MagicMock(return_value=Future())
mock.return_value.set_result(123)
result = await mock()
assert result == 123
In your case, since you're patching Service (which gets passed in as mock), mock.return_value = Future() should do the trick.

shaun shia provided really good universal solution, but i found what in python 3.8 you can use just #patch('__main__.Service', new=AsyncMock)

I found this comment very useful when trying to await a mock object in Python < 3.8. You simply create a child class AsyncMock that inherits from MagicMock and overwrites a __call__ method to be a coroutine:
class AsyncMock(MagicMock):
async def __call__(self, *args, **kwargs):
return super(AsyncMock, self).__call__(*args, **kwargs)
Then, inside your test, do:
#pytest.mark.asyncio
async def test_my_method():
# Test "my_method" coroutine by injecting an async mock
my_mock = AsyncMock()
assert await my_method(my_mock)
you might also want to install pytest-asyncio

To override async classes one needs to tell patch that the return_value needs to be AsyncMock. So use
#patch('__main__.Service', return_value=AsyncMock(Service))
def test_handle(self, mock):
loop = asyncio.get_event_loop()
app = App()
res = loop.run_until_complete(app.handle('foo'))
app.service.compute.assert_called_with("foo")
With that Service will be a MagicMock, but Service() will return an AsyncMock instance of Service.

If you'd like a pytest-mock solution compatible with < py3.8, I did something like this.
class AsyncMock(MagicMock):
async def __call__(self, *args, **kwargs):
return super().__call__(*args, **kwargs)
def test_my_method(mocker):
my_mock = mocker.patch("path.to.mocked.thing", AsyncMock())
my_mock.return_value = [1, 2, 3]
assert my_method()
Definitely borrowed from Tomasz's solution here!

Related

Mocking an Async Property in Python

The below example class has a property bar that is awaitable, as in async_main() because it (theoretically) does some IO work before returning the answer to everything.
class Foo:
#property
async def bar(self):
return 42
async def async_main():
f = Foo()
print(await f.bar)
I'm having trouble testing it, as the usual suspects of Mock, MagicMock, and AsyncMock don't work with properties as expected. My current workaround is:
f.bar = some_awaitable()
since this makes f.bar a 'field' that can be awaited, but unfortunately I need to access it multiple times while it's under test, which yields RuntimeError: cannot reuse already awaited coroutine on the second access of course.
Is there an established way to mock an async property like this?
The easiest way that I can think of is to patch bar again with an async property for the purposes of your test.
I am assuming you have some other method on Foo that you want to test, and that method calls its bar.
code.py
from asyncio import run
class Foo:
#property
async def bar(self) -> int:
return 42
async def func(self) -> int:
return await self.bar
async def main():
f = Foo()
print(await f.func())
if __name__ == '__main__':
run(main())
test.py
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch
from . import code
class FooTestCase(IsolatedAsyncioTestCase):
async def test_func(self) -> None:
expected_output = 69420
#property
async def mock_bar(_foo_self: code.Foo) -> int:
return expected_output
with patch.object(code.Foo, "bar", new=mock_bar):
f = code.Foo()
# Just to see that our mocking worked:
self.assertEqual(expected_output, await f.bar)
# Should call `bar` property again:
output = await f.func()
self.assertEqual(expected_output, output)
References: patch docs.

Python Mockito: How do I set up async mocks?

The following code works as expected for the synchronous part but gives me a TypeError for the async call (TypeError: object NoneType can't be used in 'await' expression), presumably because the mock constructor can't properly deal with the spec. How do I properly tell Mockito that it needs to set up an asynchronous mock for async_method ?
class MockedClass():
def sync_method(self):
pass
async def async_method(self):
pass
class TestedClass():
def do_something_sync(self, mocked: MockedClass):
mocked.sync_method()
async def do_something_async(self, mocked: MockedClass):
await mocked.async_method()
#pytest.mark.asyncio
async def test():
my_mock = mock(spec=MockedClass)
tested_class = TestedClass()
tested_class.do_something_sync(my_mock)
verify(my_mock).sync_method()
await tested_class.do_something_async(my_mock) # <- Fails here
verify(my_mock).async_method()
Edit:
For reference, this is how it works with the standard mocks (the behavior that I expect):
In mockito my_mock.async_method() would not return anything useful by default and without further configuration. (T.i. it returns None which is not awaitable.)
What I did in the past:
# a helper function
def future(value=None):
f = asyncio.Future()
f.set_result(value)
return f
# your code
#pytest.mark.asyncio
async def test():
my_mock = mock(spec=MockedClass)
when(my_mock).async_method().thenReturn(future(None)) # fill in whatever you expect the method to return
# ....

How to mock an Awaitable in Python tests

I'm wondering if there's a way to easily mock an Awaitable object for python tests. I know how to use AsyncMocks to create mocks which mirror coroutines (i.e. will return Awaitable objects from method calls), but am running into a bit of problem when trying to mock an Awaitable directly.
Here's an example:
import unittest
from asyncio import Task
from typing import Any
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock
async def function_using_awaitable(task: Task) -> Any:
await task
return task.result()
class TestFunction(IsolatedAsyncioTestCase):
async def test_function_using_awaitable(self):
mock_task = AsyncMock(spec=Task)
result = await function_using_awaitable(mock_task)
mock_task.assert_awaited_once()
assert result == mock_task.result.return_value
if __name__ == '__main__':
unittest.main()
which throws up:
Traceback (most recent call last):
...
File ..., line 9, in function_using_awaitable
await task
TypeError: object AsyncMock can't be used in 'await' expression
Digging into the Python docs it looks like the __await__ magic method is one of the few that isn't supported in mocks so wondering how I can go about doing this?
One solution might be to just extend the AsyncMock to be an awaitable mock object, adding await. In code, that could look like
from typing import Iterator, Any
async def function_using_awaitable(task: Task) -> Any:
await task
return task.result()
class AwaitableMock(AsyncMock):
def __await__(self) -> Iterator[Any]:
self.await_count += 1
return iter([])
class TestFunction(IsolatedAsyncioTestCase):
async def test_function_using_awaitable(self):
mock_task = AwaitableMock(spec=Task)
result = await function_using_awaitable(mock_task)
mock_task.assert_awaited_once()
assert result == mock_task.result.return_value

How to mock an async function returning a tuple?

I need to test the following function
class C:
async def f():
a, b = await self.g() # need to mock g
However, the following test got the error of TypeError: object tuple can't be used in 'await' expression
#pytest.mark.asyncio
async def test_f():
sut = C()
sut.g = MagicMock(return_value=(1,2)) # TypeError: object tuple can't be used in 'await' expression
await sut.f()
sut.g.assert_called_once()
Use AsyncMock instead of the MagicMock:
from unittest.mock import AsyncMock
#pytest.mark.asyncio
async def test_f():
sut = C()
sut.g = AsyncMock(return_value=(1,2))
await sut.f()
sut.g.assert_called_once()
AsyncMock is part of the standard library since Python 3.8; if you need a backport for older versions, install and use the asyncmock package.

Mock Python async multi function

How to mock async multi-function in python tornado framework. I want to do unit testing for the async function. inside the async function, I have an await multi() function call. how do I patch this multi-function for unit testing?
from tornado.gen import multi
async def get_session_details(request_param, docs):
sesson = await multi([doc for doc in docs])
# session result is [[],[],[],[],[]]
return session
# unit test
import pytest
from tornodo.gen import multi
import mock
#mock.patch(tornoda.gen.multi)
async def test_get_session(mock_multi)
mock_multi.return_value = [[],[],[],[],[]]
session = get_session_detals(request_param, docs)
assert session[0][session] is None
# The above code I tried but mock multi is not patching with get_session_details.
You can try using
mocker.patch("tornodo.gen.tornodo.gen.multi", return_value = [[],[],[],[],[]])
async def test_get_session(mocker, mock_multi)
mocker.patch("tornodo.gen.tornodo.gen.multi", return_value = [[],[],[],[],[]])
session = get_session_detals(request_param, docs)
assert session[0][session] is None
mocker is availble in pytest-mock,
https://pypi.org/project/pytest-mock/
First, I suggest you to use tornado.testing.AsyncTestCase and gen_test to test async code:
# unit test
from tornado.testing import AsyncTestCase, gen_test
import mock
from your_file import get_session_details
class ExampleTest(AsyncTestCase):
#gen_test
def test_get_session_details(self):
yield get_session_details(None, [])
Second, if you want to mock a async function, try this:
# unit test
import asyncio
import mock
from tornado.testing import AsyncTestCase, gen_test
from your_file import get_session_details
def get_fake_future(return_value: Any = None) -> asyncio.Future:
"""Make a Future object with a expected return value.
Args:
return_value (any): the result of the future
"""
fake_future = asyncio.Future()
fake_future.set_result(return_value)
return fake_future
class ExampleTest(AsyncTestCase):
#mock.patch("your_file.multi", return_value=get_fake_future())
#gen_test
def test_get_session_details(self):
yield get_session_details(None, [])

Categories