@@ -68,7 +68,7 @@ available, and then make assertions about how they have been used:
68
68
3
69
69
>>> thing.method.assert_called_with(3 , 4 , 5 , key = ' value' )
70
70
71
- :attr: `side_effect ` allows you to perform side effects, including raising an
71
+ :attr: `~Mock. side_effect ` allows you to perform side effects, including raising an
72
72
exception when a mock is called:
73
73
74
74
>>> from unittest.mock import Mock
@@ -756,16 +756,16 @@ the *new_callable* argument to :func:`patch`.
756
756
757
757
.. attribute :: __class__
758
758
759
- Normally the :attr: `__class__ ` attribute of an object will return its type.
760
- For a mock object with a :attr: `spec `, `` __class__ ` ` returns the spec class
759
+ Normally the :attr: `! __class__ ` attribute of an object will return its type.
760
+ For a mock object with a :attr: `! spec `, :attr: ` ! __class__ ` returns the spec class
761
761
instead. This allows mock objects to pass :func: `isinstance ` tests for the
762
762
object they are replacing / masquerading as:
763
763
764
764
>>> mock = Mock(spec = 3 )
765
765
>>> isinstance (mock, int )
766
766
True
767
767
768
- :attr: `__class__ ` is assignable to, this allows a mock to pass an
768
+ :attr: `! __class__ ` is assignable to, this allows a mock to pass an
769
769
:func: `isinstance ` check without forcing you to use a spec:
770
770
771
771
>>> mock = Mock()
@@ -779,8 +779,8 @@ the *new_callable* argument to :func:`patch`.
779
779
meaning of :class: `Mock `, with the exception of *return_value * and *side_effect *
780
780
which have no meaning on a non-callable mock.
781
781
782
- Mock objects that use a class or an instance as a :attr: `spec ` or
783
- :attr: `spec_set ` are able to pass :func: `isinstance ` tests:
782
+ Mock objects that use a class or an instance as a :attr: `! spec ` or
783
+ :attr: `! spec_set ` are able to pass :func: `isinstance ` tests:
784
784
785
785
>>> mock = Mock(spec = SomeClass)
786
786
>>> isinstance (mock, SomeClass)
@@ -1149,7 +1149,7 @@ Calls made to the object will be recorded in the attributes
1149
1149
like :attr: `~Mock.call_args ` and :attr: `~Mock.call_args_list `.
1150
1150
1151
1151
If :attr: `~Mock.side_effect ` is set then it will be called after the call has
1152
- been recorded, so if :attr: `side_effect ` raises an exception the call is still
1152
+ been recorded, so if :attr: `! side_effect ` raises an exception the call is still
1153
1153
recorded.
1154
1154
1155
1155
The simplest way to make a mock raise an exception when called is to make
@@ -1170,8 +1170,8 @@ The simplest way to make a mock raise an exception when called is to make
1170
1170
>>> m.mock_calls
1171
1171
[call(1, 2, 3), call('two', 'three', 'four')]
1172
1172
1173
- If :attr: `side_effect ` is a function then whatever that function returns is what
1174
- calls to the mock return. The :attr: `side_effect ` function is called with the
1173
+ If :attr: `~Mock. side_effect ` is a function then whatever that function returns is what
1174
+ calls to the mock return. The :attr: `! side_effect ` function is called with the
1175
1175
same arguments as the mock. This allows you to vary the return value of the
1176
1176
call dynamically, based on the input:
1177
1177
@@ -1188,7 +1188,7 @@ call dynamically, based on the input:
1188
1188
1189
1189
If you want the mock to still return the default return value (a new mock), or
1190
1190
any set return value, then there are two ways of doing this. Either return
1191
- :attr: `mock .return_value ` from inside :attr: `side_effect `, or return :data: `DEFAULT `:
1191
+ :attr: `~Mock .return_value ` from inside :attr: `~Mock. side_effect `, or return :data: `DEFAULT `:
1192
1192
1193
1193
>>> m = MagicMock()
1194
1194
>>> def side_effect (* args , ** kwargs ):
@@ -1205,8 +1205,8 @@ any set return value, then there are two ways of doing this. Either return
1205
1205
>>> m()
1206
1206
3
1207
1207
1208
- To remove a :attr: `side_effect `, and return to the default behaviour, set the
1209
- :attr: `side_effect ` to ``None ``:
1208
+ To remove a :attr: `~Mock. side_effect `, and return to the default behaviour, set the
1209
+ :attr: `! side_effect ` to ``None ``:
1210
1210
1211
1211
>>> m = MagicMock(return_value = 6 )
1212
1212
>>> def side_effect (* args , ** kwargs ):
@@ -1219,7 +1219,7 @@ To remove a :attr:`side_effect`, and return to the default behaviour, set the
1219
1219
>>> m()
1220
1220
6
1221
1221
1222
- The :attr: `side_effect ` can also be any iterable object. Repeated calls to the mock
1222
+ The :attr: `~Mock. side_effect ` can also be any iterable object. Repeated calls to the mock
1223
1223
will return values from the iterable (until the iterable is exhausted and
1224
1224
a :exc: `StopIteration ` is raised):
1225
1225
@@ -1260,7 +1260,7 @@ objects of any type.
1260
1260
1261
1261
You may want a mock object to return ``False `` to a :func: `hasattr ` call, or raise an
1262
1262
:exc: `AttributeError ` when an attribute is fetched. You can do this by providing
1263
- an object as a :attr: `spec ` for a mock, but that isn't always convenient.
1263
+ an object as a :attr: `! spec ` for a mock, but that isn't always convenient.
1264
1264
1265
1265
You "block" attributes by deleting them. Once deleted, accessing an attribute
1266
1266
will raise an :exc: `AttributeError `.
@@ -1429,7 +1429,7 @@ patch
1429
1429
If you are patching builtins in a module then you don't
1430
1430
need to pass ``create=True ``, it will be added by default.
1431
1431
1432
- Patch can be used as a :class: `TestCase ` class decorator. It works by
1432
+ Patch can be used as a :class: `~unittest. TestCase ` class decorator. It works by
1433
1433
decorating each test method in the class. This reduces the boilerplate
1434
1434
code when your test methods share a common patchings set. :func: `patch ` finds
1435
1435
tests by looking for method names that start with ``patch.TEST_PREFIX ``.
@@ -1467,7 +1467,7 @@ If the class is instantiated multiple times you could use
1467
1467
can set the *return_value * to be anything you want.
1468
1468
1469
1469
To configure return values on methods of *instances * on the patched class
1470
- you must do this on the :attr: `return_value `. For example::
1470
+ you must do this on the :attr: `~Mock. return_value `. For example::
1471
1471
1472
1472
>>> class Class:
1473
1473
... def method(self):
@@ -1788,13 +1788,13 @@ context manager is a dictionary where created mocks are keyed by name::
1788
1788
patch methods: start and stop
1789
1789
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1790
1790
1791
- All the patchers have :meth: `start ` and :meth: `stop ` methods. These make it simpler to do
1791
+ All the patchers have :meth: `! start ` and :meth: `! stop ` methods. These make it simpler to do
1792
1792
patching in ``setUp `` methods or where you want to do multiple patches without
1793
1793
nesting decorators or with statements.
1794
1794
1795
1795
To use them call :func: `patch `, :func: `patch.object ` or :func: `patch.dict ` as
1796
1796
normal and keep a reference to the returned ``patcher `` object. You can then
1797
- call :meth: `start ` to put the patch in place and :meth: `stop ` to undo it.
1797
+ call :meth: `! start ` to put the patch in place and :meth: `! stop ` to undo it.
1798
1798
1799
1799
If you are using :func: `patch ` to create a mock for you then it will be returned by
1800
1800
the call to ``patcher.start ``. ::
@@ -1811,7 +1811,7 @@ the call to ``patcher.start``. ::
1811
1811
1812
1812
1813
1813
A typical use case for this might be for doing multiple patches in the ``setUp ``
1814
- method of a :class: `TestCase `::
1814
+ method of a :class: `~unittest. TestCase `::
1815
1815
1816
1816
>>> class MyTest(unittest.TestCase):
1817
1817
... def setUp(self):
@@ -2484,7 +2484,7 @@ behaviour you can switch it off by setting the module level switch
2484
2484
2485
2485
Alternatively you can just use ``vars(my_mock) `` (instance members) and
2486
2486
``dir(type(my_mock)) `` (type members) to bypass the filtering irrespective of
2487
- :const: `mock. FILTER_DIR `.
2487
+ :const: `FILTER_DIR `.
2488
2488
2489
2489
2490
2490
mock_open
@@ -2499,7 +2499,7 @@ mock_open
2499
2499
default) then a :class: `MagicMock ` will be created for you, with the API limited
2500
2500
to methods or attributes available on standard file handles.
2501
2501
2502
- *read_data * is a string for the :meth: `~io.IOBase .read `,
2502
+ *read_data * is a string for the :meth: `~io.RawIOBase .read `,
2503
2503
:meth: `~io.IOBase.readline `, and :meth: `~io.IOBase.readlines ` methods
2504
2504
of the file handle to return. Calls to those methods will take data from
2505
2505
*read_data * until it is depleted. The mock of these methods is pretty
@@ -2511,7 +2511,7 @@ mock_open
2511
2511
2512
2512
.. versionchanged :: 3.4
2513
2513
Added :meth: `~io.IOBase.readline ` and :meth: `~io.IOBase.readlines ` support.
2514
- The mock of :meth: `~io.IOBase .read ` changed to consume *read_data * rather
2514
+ The mock of :meth: `~io.RawIOBase .read ` changed to consume *read_data * rather
2515
2515
than returning it on each call.
2516
2516
2517
2517
.. versionchanged :: 3.5
@@ -2563,7 +2563,7 @@ And for reading files::
2563
2563
Autospeccing
2564
2564
~~~~~~~~~~~~
2565
2565
2566
- Autospeccing is based on the existing :attr: `spec ` feature of mock. It limits the
2566
+ Autospeccing is based on the existing :attr: `! spec ` feature of mock. It limits the
2567
2567
api of mocks to the api of an original object (the spec), but it is recursive
2568
2568
(implemented lazily) so that attributes of mocks only have the same api as
2569
2569
the attributes of the spec. In addition mocked functions / methods have the
@@ -2588,8 +2588,8 @@ unit tests. Testing everything in isolation is all fine and dandy, but if you
2588
2588
don't test how your units are "wired together" there is still lots of room
2589
2589
for bugs that tests might have caught.
2590
2590
2591
- :mod: `mock ` already provides a feature to help with this, called speccing. If you
2592
- use a class or instance as the :attr: `spec ` for a mock then you can only access
2591
+ :mod: `unittest. mock ` already provides a feature to help with this, called speccing. If you
2592
+ use a class or instance as the :attr: `! spec ` for a mock then you can only access
2593
2593
attributes on the mock that exist on the real class:
2594
2594
2595
2595
>>> from urllib import request
@@ -2627,7 +2627,7 @@ Here's an example of it in use::
2627
2627
>>> mock_request.Request
2628
2628
<MagicMock name='request.Request' spec='Request' id='...'>
2629
2629
2630
- You can see that :class: `request.Request ` has a spec. :class: `request.Request ` takes two
2630
+ You can see that :class: `! request.Request ` has a spec. :class: `! request.Request ` takes two
2631
2631
arguments in the constructor (one of which is *self *). Here's what happens if
2632
2632
we try to call it incorrectly::
2633
2633
@@ -2643,8 +2643,8 @@ specced mocks)::
2643
2643
>>> req
2644
2644
<NonCallableMagicMock name='request.Request()' spec='Request' id='...'>
2645
2645
2646
- :class: `Request ` objects are not callable, so the return value of instantiating our
2647
- mocked out :class: `request.Request ` is a non-callable mock. With the spec in place
2646
+ :class: `! Request ` objects are not callable, so the return value of instantiating our
2647
+ mocked out :class: `! request.Request ` is a non-callable mock. With the spec in place
2648
2648
any typos in our asserts will raise the correct error::
2649
2649
2650
2650
>>> req.add_header('spam', 'eggs')
@@ -2796,8 +2796,8 @@ Sealing mocks
2796
2796
.. versionadded :: 3.7
2797
2797
2798
2798
2799
- Order of precedence of :attr: `side_effect `, :attr: `return_value ` and *wraps *
2800
- ----------------------------------------------------------------------------
2799
+ Order of precedence of :attr: `! side_effect `, :attr: `! return_value ` and *wraps *
2800
+ ------------------------------------------------------------------------------
2801
2801
2802
2802
The order of their precedence is:
2803
2803
0 commit comments