diff options
author | Mario Corchero <mcorcherojim@bloomberg.net> | 2019-09-09 14:18:06 (GMT) |
---|---|---|
committer | Lisa Roach <lisaroach14@gmail.com> | 2019-09-09 14:18:06 (GMT) |
commit | f5e7f39d2916ed150e80381faed125f405a11e11 (patch) | |
tree | d0304a363837b624ac4c141075015ebec04fa7ae /Doc/library | |
parent | fa3a38d81faaf96d17b5a7f0248b9923e3a648cc (diff) | |
download | cpython-f5e7f39d2916ed150e80381faed125f405a11e11.zip cpython-f5e7f39d2916ed150e80381faed125f405a11e11.tar.gz cpython-f5e7f39d2916ed150e80381faed125f405a11e11.tar.bz2 |
docs: Add references to AsyncMock in unittest.mock.patch (#13681)
Update the docs as patch can now return an AsyncMock if the patched
object is an async function.
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/unittest.mock.rst | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst index 304ba53..9bb3427 100644 --- a/Doc/library/unittest.mock.rst +++ b/Doc/library/unittest.mock.rst @@ -1307,8 +1307,10 @@ patch is patched with a *new* object. When the function/with statement exits the patch is undone. - If *new* is omitted, then the target is replaced with a - :class:`MagicMock`. If :func:`patch` is used as a decorator and *new* is + If *new* is omitted, then the target is replaced with an + :class:`AsyncMock` if the patched object is an async function or + a :class:`MagicMock` otherwise. + If :func:`patch` is used as a decorator and *new* is omitted, the created mock is passed in as an extra argument to the decorated function. If :func:`patch` is used as a context manager the created mock is returned by the context manager. @@ -1326,8 +1328,8 @@ patch patch to pass in the object being mocked as the spec/spec_set object. *new_callable* allows you to specify a different class, or callable object, - that will be called to create the *new* object. By default :class:`MagicMock` is - used. + that will be called to create the *new* object. By default :class:`AsyncMock` + is used for async functions and :class:`MagicMock` for the rest. A more powerful form of *spec* is *autospec*. If you set ``autospec=True`` then the mock will be created with a spec from the object being replaced. @@ -1491,6 +1493,10 @@ work as expected:: ... >>> test() +.. versionchanged:: 3.8 + + :func:`patch` now returns an :class:`AsyncMock` if the target is an async function. + patch.object ~~~~~~~~~~~~ @@ -2275,6 +2281,12 @@ See :ref:`auto-speccing` for examples of how to use auto-speccing with :func:`create_autospec` and the *autospec* argument to :func:`patch`. +.. versionchanged:: 3.8 + + :func:`create_autospec` now returns an :class:`AsyncMock` if the target is + an async function. + + ANY ~~~ |