8000 bpo-37052: Add examples for mocking async iterators and context manag… · python/cpython@c8dfa73 · GitHub
[go: up one dir, main page]

Skip to content

Commit c8dfa73

Browse files
tirkarthimiss-islington
authored andcommitted
bpo-37052: Add examples for mocking async iterators and context managers (GH-14660)
Add examples for mocking asynchronous iterators and asynchronous context managers. https://bugs.python.org/issue37052
1 parent bd0c7a1 commit c8dfa73

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Doc/library/unittest.mock-examples.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
.. testsetup::
1414

15+
import asyncio
1516
import unittest
1617
from unittest.mock import Mock, MagicMock, patch, call, sentinel
1718

@@ -276,6 +277,44 @@ function returns is what the call returns:
276277
2
277278

278279

280+
Mocking asynchronous iterators
281+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282+
283+
Since Python 3.8, ``MagicMock`` has support to mock :ref:`async-iterators`
284+
through ``__aiter__``. The :attr:`~Mock.return_value` attribute of ``__aiter__``
285+
can be used to set the return values to be used for iteration.
286+
287+
>>> mock = MagicMock()
288+
>>> mock.__aiter__.return_value = [1, 2, 3]
289+
>>> async def main():
290+
... return [i async for i in mock]
291+
>>> asyncio.run(main())
292+
[1, 2, 3]
293+
294+
295+
Mocking asynchronous context manager
296+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
297+
298+
Since Python 3.8, ``MagicMock`` has support to mock
299+
:ref:`async-context-managers` through ``__aenter__`` and ``__aexit__``. The
300+
return value of ``__aenter__`` is an :class:`AsyncMock`.
301+
302+
>>> class AsyncContextManager:
303+
...
304+
... async def __aenter__(self):
305+
... return self
306+
...
307+
... async def __aexit__(self):
308+
... pass
309+
>>> mock_instance = MagicMock(AsyncContextManager())
310+
>>> async def main():
311+
... async with mock_instance as result:
312+
... pass
313+
>>> asyncio.run(main())
314+
>>> mock_instance.__aenter__.assert_called_once()
315+
>>> mock_instance.__aexit__.assert_called_once()
316+
317+
279318
Creating a Mock from an Existing Object
280319
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
281320

0 commit comments

Comments
 (0)
0