Enhance :class:`Mock` with features allowing to mock an async function. 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() >>> inspect.iscoroutinefunction(mock) Tr
| 2481 | |
| 2482 | |
| 2483 | class AsyncMock(AsyncMockMixin, AsyncMagicMixin, Mock): |
| 2484 | """ |
| 2485 | Enhance :class:`Mock` with features allowing to mock |
| 2486 | an async function. |
| 2487 | |
| 2488 | The :class:`AsyncMock` object will behave so the object is |
| 2489 | recognized as an async function, and the result of a call is an awaitable: |
| 2490 | |
| 2491 | >>> mock = AsyncMock() |
| 2492 | >>> inspect.iscoroutinefunction(mock) |
| 2493 | True |
| 2494 | >>> inspect.isawaitable(mock()) |
| 2495 | True |
| 2496 | |
| 2497 | |
| 2498 | The result of ``mock()`` is an async function which will have the outcome |
| 2499 | of ``side_effect`` or ``return_value``: |
| 2500 | |
| 2501 | - if ``side_effect`` is a function, the async function will return the |
| 2502 | result of that function, |
| 2503 | - if ``side_effect`` is an exception, the async function will raise the |
| 2504 | exception, |
| 2505 | - if ``side_effect`` is an iterable, the async function will return the |
| 2506 | next value of the iterable, however, if the sequence of result is |
| 2507 | exhausted, ``StopIteration`` is raised immediately, |
| 2508 | - if ``side_effect`` is not defined, the async function will return the |
| 2509 | value defined by ``return_value``, hence, by default, the async function |
| 2510 | returns a new :class:`AsyncMock` object. |
| 2511 | |
| 2512 | If the outcome of ``side_effect`` or ``return_value`` is an async function, |
| 2513 | the mock async function obtained when the mock object is called will be this |
| 2514 | async function itself (and not an async function returning an async |
| 2515 | function). |
| 2516 | |
| 2517 | The test author can also specify a wrapped object with ``wraps``. In this |
| 2518 | case, the :class:`Mock` object behavior is the same as with an |
| 2519 | :class:`.Mock` object: the wrapped object may have methods |
| 2520 | defined as async function functions. |
| 2521 | |
| 2522 | Based on Martin Richard's asynctest project. |
| 2523 | """ |
| 2524 | |
| 2525 | |
| 2526 | class _ANY(object): |
no outgoing calls
searching dependent graphs…