@@ -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
@@ -760,16 +760,16 @@ the *new_callable* argument to :func:`patch`.
760
760
761
761
.. attribute :: __class__
762
762
763
- Normally the :attr: `__class__ ` attribute of an object will return its type.
764
- For a mock object with a :attr: `spec `, `` __class__ ` ` returns the spec class
763
+ Normally the :attr: `! __class__ ` attribute of an object will return its type.
764
+ For a mock object with a :attr: `! spec `, :attr: ` ! __class__ ` returns the spec class
765
765
instead. This allows mock objects to pass :func: `isinstance ` tests for the
766
766
object they are replacing / masquerading as:
767
767
768
768
>>> mock = Mock(spec = 3 )
769
769
>>> isinstance (mock, int )
770
770
True
771
771
772
- :attr: `__class__ ` is assignable to, this allows a mock to pass an
772
+ :attr: `! __class__ ` is assignable to, this allows a mock to pass an
773
773
:func: `isinstance ` check without forcing you to use a spec:
774
774
775
775
>>> mock = Mock()
@@ -783,8 +783,8 @@ the *new_callable* argument to :func:`patch`.
783
783
meaning of :class: `Mock `, with the exception of *return_value * and *side_effect *
784
784
which have no meaning on a non-callable mock.
785
785
786
- Mock objects that use a class or an instance as a :attr: `spec ` or
787
- :attr: `spec_set ` are able to pass :func: `isinstance ` tests:
786
+ Mock objects that use a class or an instance as a :attr: `! spec ` or
787
+ :attr: `! spec_set ` are able to pass :func: `isinstance ` tests:
788
788
789
789
>>> mock = Mock(spec = SomeClass)
790
790
>>> isinstance (mock, SomeClass)
@@ -1198,7 +1198,7 @@ Calls made to the object will be recorded in the attributes
1198
1198
like :attr: `~Mock.call_args ` and :attr: `~Mock.call_args_list `.
1199
1199
1200
1200
If :attr: `~Mock.side_effect ` is set then it will be called after the call has
1201
- been recorded, so if :attr: `side_effect ` raises an exception the call is still
1201
+ been recorded, so if :attr: `! side_effect ` raises an exception the call is still
1202
1202
recorded.
1203
1203
1204
1204
The simplest way to make a mock raise an exception when called is to make
@@ -1219,8 +1219,8 @@ The simplest way to make a mock raise an exception when called is to make
1219
1219
>>> m.mock_calls
1220
1220
[call(1, 2, 3), call('two', 'three', 'four')]
1221
1221
1222
- If :attr: `side_effect ` is a function then whatever that function returns is what
1223
- calls to the mock return. The :attr: `side_effect ` function is called with the
1222
+ If :attr: `~Mock. side_effect ` is a function then whatever that function returns is what
1223
+ calls to the mock return. The :attr: `! side_effect ` function is called with the
1224
1224
same arguments as the mock. This allows you to vary the return value of the
1225
1225
call dynamically, based on the input:
1226
1226
@@ -1237,7 +1237,7 @@ call dynamically, based on the input:
1237
1237
1238
1238
If you want the mock to still return the default return value (a new mock), or
1239
1239
any set return value, then there are two ways of doing this. Either return
1240
- :attr: `mock .return_value ` from inside :attr: `side_effect `, or return :data: `DEFAULT `:
1240
+ :attr: `~Mock .return_value ` from inside :attr: `~Mock. side_effect `, or return :data: `DEFAULT `:
1241
1241
1242
1242
>>> m = MagicMock()
1243
1243
>>> def side_effect (* args , ** kwargs ):
@@ -1254,8 +1254,8 @@ any set return value, then there are two ways of doing this. Either return
1254
1254
>>> m()
1255
1255
3
1256
1256
1257
- To remove a :attr: `side_effect `, and return to the default behaviour, set the
1258
- :attr: `side_effect ` to ``None ``:
1257
+ To remove a :attr: `~Mock. side_effect `, and return to the default behaviour, set the
1258
+ :attr: `! side_effect ` to ``None ``:
1259
1259
1260
1260
>>> m = MagicMock(return_value = 6 )
1261
1261
>>> def side_effect (* args , ** kwargs ):
@@ -1268,7 +1268,7 @@ To remove a :attr:`side_effect`, and return to the default behaviour, set the
1268
1268
>>> m()
1269
1269
6
1270
1270
1271
- The :attr: `side_effect ` can also be any iterable object. Repeated calls to the mock
1271
+ The :attr: `~Mock. side_effect ` can also be any iterable object. Repeated calls to the mock
1272
1272
will return values from the iterable (until the iterable is exhausted and
1273
1273
a :exc: `StopIteration ` is raised):
1274
1274
@@ -1309,7 +1309,7 @@ objects of any type.
1309
1309
1310
1310
You may want a mock object to return ``False `` to a :func: `hasattr ` call, or raise an
1311
1311
:exc: `AttributeError ` when an attribute is fetched. You can do this by providing
1312
- an object as a :attr: `spec ` for a mock, but that isn't always convenient.
1312
+ an object as a :attr: `! spec ` for a mock, but that isn't always convenient.
1313
1313
1314
1314
You "block" attributes by deleting them. Once deleted, accessing an attribute
1315
1315
will raise an :exc: `AttributeError `.
@@ -1478,7 +1478,7 @@ patch
1478
1478
If you are patching builtins in a module then you don't
1479
1479
need to pass ``create=True ``, it will be added by default.
1480
1480
1481
- Patch can be used as a :class: `TestCase ` class decorator. It works by
1481
+ Patch can be used as a :class: `~unittest. TestCase ` class decorator. It works by
1482
1482
decorating each test method in the class. This reduces the boilerplate
1483
1483
code when your test methods share a common patchings set. :func: `patch ` finds
1484
1484
tests by looking for method names that start with ``patch.TEST_PREFIX ``.
@@ -1516,7 +1516,7 @@ If the class is instantiated multiple times you could use
1516
1516
can set the *return_value * to be anything you want.
1517
1517
1518
1518
To configure return values on methods of *instances * on the patched class
1519
- you must do this on the :attr: `return_value `. For example::
1519
+ you must do this on the :attr: `~Mock. return_value `. For example::
1520
1520
1521
1521
>>> class Class:
1522
1522
... def method(self):
@@ -1838,13 +1838,13 @@ context manager is a dictionary where created mocks are keyed by name::
1838
1838
patch methods: start and stop
1839
1839
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1840
1840
1841
- All the patchers have :meth: `start ` and :meth: `stop ` methods. These make it simpler to do
1841
+ All the patchers have :meth: `! start ` and :meth: `! stop ` methods. These make it simpler to do
1842
1842
patching in ``setUp `` methods or where you want to do multiple patches without
1843
1843
nesting decorators or with statements.
1844
1844
1845
1845
To use them call :func: `patch `, :func: `patch.object ` or :func: `patch.dict ` as
1846
1846
normal and keep a reference to the returned ``patcher `` object. You can then
1847
- call :meth: `start ` to put the patch in place and :meth: `stop ` to undo it.
1847
+ call :meth: `! start ` to put the patch in place and :meth: `! stop ` to undo it.
1848
1848
1849
1849
If you are using :func: `patch ` to create a mock for you then it will be returned by
1850
1850
the call to ``patcher.start ``. ::
@@ -1861,7 +1861,7 @@ the call to ``patcher.start``. ::
1861
1861
1862
1862
1863
1863
A typical use case for this might be for doing multiple patches in the ``setUp ``
1864
- method of a :class: `TestCase `::
1864
+ method of a :class: `~unittest. TestCase `::
1865
1865
1866
1866
>>> class MyTest(unittest.TestCase):
1867
1867
... def setUp(self):
@@ -2534,7 +2534,7 @@ behaviour you can switch it off by setting the module level switch
2534
2534
2535
2535
Alternatively you can just use ``vars(my_mock) `` (instance members) and
2536
2536
``dir(type(my_mock)) `` (type members) to bypass the filtering irrespective of
2537
- :const: `mock. FILTER_DIR `.
2537
+ :const: `FILTER_DIR `.
2538
2538
2539
2539
2540
2540
mock_open
@@ -2549,7 +2549,7 @@ mock_open
2549
2549
default) then a :class: `MagicMock ` will be created for you, with the API limited
2550
2550
to methods or attributes available on standard file handles.
2551
2551
2552
- *read_data * is a string for the :meth: `~io.IOBase .read `,
2552
+ *read_data * is a string for the :meth: `~io.RawIOBase .read `,
2553
2553
:meth: `~io.IOBase.readline `, and :meth: `~io.IOBase.readlines ` methods
2554
2554
of the file handle to return. Calls to those methods will take data from
2555
2555
*read_data * until it is depleted. The mock of these methods is pretty
@@ -2561,7 +2561,7 @@ mock_open
2561
2561
2562
2562
.. versionchanged :: 3.4
2563
2563
Added :meth: `~io.IOBase.readline ` and :meth: `~io.IOBase.readlines ` support.
2564
- The mock of :meth: `~io.IOBase .read ` changed to consume *read_data * rather
2564
+ The mock of :meth: `~io.RawIOBase .read ` changed to consume *read_data * rather
2565
2565
than returning it on each call.
2566
2566
2567
2567
.. versionchanged :: 3.5
@@ -2613,7 +2613,7 @@ And for reading files::
2613
2613
Autospeccing
2614
2614
~~~~~~~~~~~~
2615
2615
2616
- Autospeccing is based on the existing :attr: `spec ` feature of mock. It limits the
2616
+ Autospeccing is based on the existing :attr: `! spec ` feature of mock. It limits the
2617
2617
api of mocks to the api of an original object (the spec), but it is recursive
2618
2618
(implemented lazily) so that attributes of mocks only have the same api as
2619
2619
the attributes of the spec. In addition mocked functions / methods have the
@@ -2638,8 +2638,8 @@ unit tests. Testing everything in isolation is all fine and dandy, but if you
2638
2638
don't test how your units are "wired together" there is still lots of room
2639
2639
for bugs that tests might have caught.
2640
2640
2641
- :mod: `mock ` already provides a feature to help with this, called speccing. If you
2642
- use a class or instance as the :attr: `spec ` for a mock then you can only access
2641
+ :mod: `unittest. mock ` already provides a feature to help with this, called speccing. If you
2642
+ use a class or instance as the :attr: `! spec ` for a mock then you can only access
2643
2643
attributes on the mock that exist on the real class:
2644
2644
2645
2645
>>> from urllib import request
@@ -2677,7 +2677,7 @@ Here's an example of it in use::
2677
2677
>>> mock_request.Request
2678
2678
<MagicMock name='request.Request' spec='Request' id='...'>
2679
2679
2680
- You can see that :class: `request.Request ` has a spec. :class: `request.Request ` takes two
2680
+ You can see that :class: `! request.Request ` has a spec. :class: `! request.Request ` takes two
2681
2681
arguments in the constructor (one of which is *self *). Here's what happens if
2682
2682
we try to call it incorrectly::
2683
2683
@@ -2693,8 +2693,8 @@ specced mocks)::
2693
2693
>>> req
2694
2694
<NonCallableMagicMock name='request.Request()' spec='Request' id='...'>
2695
2695
2696
- :class: `Request ` objects are not callable, so the return value of instantiating our
2697
- mocked out :class: `request.Request ` is a non-callable mock. With the spec in place
2696
+ :class: `! Request ` objects are not callable, so the return value of instantiating our
2697
+ mocked out :class: `! request.Request ` is a non-callable mock. With the spec in place
2698
2698
any typos in our asserts will raise the correct error::
2699
2699
2700
2700
>>> req.add_header('spam', 'eggs')
@@ -2846,8 +2846,8 @@ Sealing mocks
2846
2846
.. versionadded :: 3.7
2847
2847
2848
2848
2849
- Order of precedence of :attr: `side_effect `, :attr: `return_value ` and *wraps *
2850
- ----------------------------------------------------------------------------
2849
+ Order of precedence of :attr: `! side_effect `, :attr: `! return_value ` and *wraps *
2850
+ ------------------------------------------------------------------------------
2851
2851
2852
2852
The order of their precedence is:
2853
2853
0 commit comments