Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24)

Side by Side Diff: Doc/library/unittest.mock.rst

Issue 18758: Fix internal references in the documentation
Patch Set: Created 6 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/library/time.rst ('k') | Doc/library/warnings.rst » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 :mod:`unittest.mock` --- mock object library 2 :mod:`unittest.mock` --- mock object library
3 ============================================ 3 ============================================
4 4
5 .. module:: unittest.mock 5 .. module:: unittest.mock
6 :synopsis: Mock object library. 6 :synopsis: Mock object library.
7 .. moduleauthor:: Michael Foord <michael@python.org> 7 .. moduleauthor:: Michael Foord <michael@python.org>
8 .. currentmodule:: unittest.mock 8 .. currentmodule:: unittest.mock
9 9
10 .. versionadded:: 3.3 10 .. versionadded:: 3.3
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 Create a new `Mock` object. `Mock` takes several optional arguments 203 Create a new `Mock` object. `Mock` takes several optional arguments
204 that specify the behaviour of the Mock object: 204 that specify the behaviour of the Mock object:
205 205
206 * `spec`: This can be either a list of strings or an existing object (a 206 * `spec`: This can be either a list of strings or an existing object (a
207 class or instance) that acts as the specification for the mock object. If 207 class or instance) that acts as the specification for the mock object. If
208 you pass in an object then a list of strings is formed by calling dir on 208 you pass in an object then a list of strings is formed by calling dir on
209 the object (excluding unsupported magic attributes and methods). 209 the object (excluding unsupported magic attributes and methods).
210 Accessing any attribute not in this list will raise an `AttributeError`. 210 Accessing any attribute not in this list will raise an `AttributeError`.
211 211
212 If `spec` is an object (rather than a list of strings) then 212 If `spec` is an object (rather than a list of strings) then
213 :attr:`__class__` returns the class of the spec object. This allows mocks 213 :attr:`~instance.__class__` returns the class of the spec object. This all ows mocks
214 to pass `isinstance` tests. 214 to pass `isinstance` tests.
215 215
216 * `spec_set`: A stricter variant of `spec`. If used, attempting to *set* 216 * `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
217 or get an attribute on the mock that isn't on the object passed as 217 or get an attribute on the mock that isn't on the object passed as
218 `spec_set` will raise an `AttributeError`. 218 `spec_set` will raise an `AttributeError`.
219 219
220 * `side_effect`: A function to be called whenever the Mock is called. See 220 * `side_effect`: A function to be called whenever the Mock is called. See
221 the :attr:`~Mock.side_effect` attribute. Useful for raising exceptions or 221 the :attr:`~Mock.side_effect` attribute. Useful for raising exceptions or
222 dynamically changing return values. The function is called with the same 222 dynamically changing return values. The function is called with the same
223 arguments as the mock, and unless it returns :data:`DEFAULT`, the return 223 arguments as the mock, and unless it returns :data:`DEFAULT`, the return
(...skipping 1758 matching lines...) Expand 10 before | Expand all | Expand 10 after
1982 1982
1983 .. function:: mock_open(mock=None, read_data=None) 1983 .. function:: mock_open(mock=None, read_data=None)
1984 1984
1985 A helper function to create a mock to replace the use of `open`. It works 1985 A helper function to create a mock to replace the use of `open`. It works
1986 for `open` called directly or used as a context manager. 1986 for `open` called directly or used as a context manager.
1987 1987
1988 The `mock` argument is the mock object to configure. If `None` (the 1988 The `mock` argument is the mock object to configure. If `None` (the
1989 default) then a `MagicMock` will be created for you, with the API limited 1989 default) then a `MagicMock` will be created for you, with the API limited
1990 to methods or attributes available on standard file handles. 1990 to methods or attributes available on standard file handles.
1991 1991
1992 `read_data` is a string for the `read`, `readline`, and `readlines` methods 1992 `read_data` is a string for the :meth:`~io.IOBase.read`,
1993 :meth:`~io.IOBase.readline`, and :meth:`~io.IOBase.readlines` methods
1993 of the file handle to return. Calls to those methods will take data from 1994 of the file handle to return. Calls to those methods will take data from
1994 `read_data` until it is depleted. The mock of these methods is pretty 1995 `read_data` until it is depleted. The mock of these methods is pretty
1995 simplistic. If you need more control over the data that you are feeding to 1996 simplistic. If you need more control over the data that you are feeding to
1996 the tested code you will need to customize this mock for yourself. 1997 the tested code you will need to customize this mock for yourself.
1997 `read_data` is an empty string by default. 1998 `read_data` is an empty string by default.
1998 1999
1999 Using `open` as a context manager is a great way to ensure your file handles 2000 Using `open` as a context manager is a great way to ensure your file handles
2000 are closed properly and is becoming common:: 2001 are closed properly and is becoming common::
2001 2002
2002 with open('/some/path', 'w') as f: 2003 with open('/some/path', 'w') as f:
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
2259 >>> p = patch('__main__.Something', autospec=SomethingForTest) 2260 >>> p = patch('__main__.Something', autospec=SomethingForTest)
2260 >>> mock = p.start() 2261 >>> mock = p.start()
2261 >>> mock.a 2262 >>> mock.a
2262 <NonCallableMagicMock name='Something.a' spec='int' id='...'> 2263 <NonCallableMagicMock name='Something.a' spec='int' id='...'>
2263 2264
2264 2265
2265 .. [#] This only applies to classes or already instantiated objects. Calling 2266 .. [#] This only applies to classes or already instantiated objects. Calling
2266 a mocked class to create a mock instance *does not* create a real instance. 2267 a mocked class to create a mock instance *does not* create a real instance.
2267 It is only attribute lookups - along with calls to `dir` - that are done. 2268 It is only attribute lookups - along with calls to `dir` - that are done.
2268 2269
OLDNEW
« no previous file with comments | « Doc/library/time.rst ('k') | Doc/library/warnings.rst » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+