summaryrefslogtreecommitdiffstats
path: root/Doc/library/unittest.mock.rst
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2014-10-31 09:39:29 (GMT)
committerGeorg Brandl <georg@python.org>2014-10-31 09:39:29 (GMT)
commite21a531ef10e120a94434b3d4806a8106dd6f271 (patch)
tree0b1d0245388effa5cbf130744b50af6ab6a1eb5f /Doc/library/unittest.mock.rst
parentfe98180bced16be7d62b762cc85d400f929d0885 (diff)
parenta4c8c47961305487ef6c40a6d882bb956f2c5a0b (diff)
downloadcpython-e21a531ef10e120a94434b3d4806a8106dd6f271.zip
cpython-e21a531ef10e120a94434b3d4806a8106dd6f271.tar.gz
cpython-e21a531ef10e120a94434b3d4806a8106dd6f271.tar.bz2
merge with 3.4
Diffstat (limited to 'Doc/library/unittest.mock.rst')
-rw-r--r--Doc/library/unittest.mock.rst15
1 files changed, 8 insertions, 7 deletions
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 44a4503..94bb731 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -481,7 +481,7 @@ the *new_callable* argument to :func:`patch`.
.. attribute:: side_effect
This can either be a function to be called when the mock is called,
- or an exception (class or instance) to be raised.
+ an iterable or an exception (class or instance) to be raised.
If you pass in a function it will be called with same arguments as the
mock and unless the function returns the :data:`DEFAULT` singleton the
@@ -489,6 +489,11 @@ the *new_callable* argument to :func:`patch`.
function returns :data:`DEFAULT` then the mock will return its normal
value (from the :attr:`return_value`).
+ If you pass in an iterable, it is used to retrieve an iterator which
+ must yield a value on every call. This value can either be an exception
+ instance to be raised, or a value to be returned from the call to the
+ mock (:data:`DEFAULT` handling is identical to the function case).
+
An example of a mock that raises an exception (to test exception
handling of an API):
@@ -506,11 +511,7 @@ the *new_callable* argument to :func:`patch`.
>>> mock(), mock(), mock()
(3, 2, 1)
- The :attr:`side_effect` function is called with the same arguments as the
- mock (so it is wise for it to take arbitrary args and keyword
- arguments) and whatever it returns is used as the return value for
- the call. The exception is if :attr:`side_effect` returns :data:`DEFAULT`,
- in which case the normal :attr:`return_value` is used.
+ Using a callable:
>>> mock = Mock(return_value=3)
>>> def side_effect(*args, **kwargs):
@@ -1031,7 +1032,7 @@ patch
used.
A more powerful form of *spec* is *autospec*. If you set ``autospec=True``
- then the mock with be created with a spec from the object being replaced.
+ then the mock will be created with a spec from the object being replaced.
All attributes of the mock will also have the spec of the corresponding
attribute of the object being replaced. Methods and functions being mocked
will have their arguments checked and will raise a :exc:`TypeError` if they are