MCPcopy Index your code
hub / github.com/python/cpython / Mock

Class Mock

Lib/unittest/mock.py:1265–1321  ·  view source on GitHub ↗

Create a new `Mock` object. `Mock` takes several optional arguments that specify the behaviour of the Mock object: * `spec`: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. If you pass in a

Source from the content-addressed store, hash-verified

1263
1264
1265class Mock(CallableMixin, NonCallableMock):
1266 """
1267 Create a new `Mock` object. `Mock` takes several optional arguments
1268 that specify the behaviour of the Mock object:
1269
1270 * `spec`: This can be either a list of strings or an existing object (a
1271 class or instance) that acts as the specification for the mock object. If
1272 you pass in an object then a list of strings is formed by calling dir on
1273 the object (excluding unsupported magic attributes and methods). Accessing
1274 any attribute not in this list will raise an `AttributeError`.
1275
1276 If `spec` is an object (rather than a list of strings) then
1277 `mock.__class__` returns the class of the spec object. This allows mocks
1278 to pass `isinstance` tests.
1279
1280 * `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
1281 or get an attribute on the mock that isn't on the object passed as
1282 `spec_set` will raise an `AttributeError`.
1283
1284 * `side_effect`: A function to be called whenever the Mock is called. See
1285 the `side_effect` attribute. Useful for raising exceptions or
1286 dynamically changing return values. The function is called with the same
1287 arguments as the mock, and unless it returns `DEFAULT`, the return
1288 value of this function is used as the return value.
1289
1290 If `side_effect` is an iterable then each call to the mock will return
1291 the next value from the iterable. If any of the members of the iterable
1292 are exceptions they will be raised instead of returned.
1293
1294 * `return_value`: The value returned when the mock is called. By default
1295 this is a new Mock (created on first access). See the
1296 `return_value` attribute.
1297
1298 * `unsafe`: By default, accessing any attribute whose name starts with
1299 *assert*, *assret*, *asert*, *aseert*, or *assrt* raises an AttributeError.
1300 Additionally, an AttributeError is raised when accessing
1301 attributes that match the name of an assertion method without the prefix
1302 `assert_`, e.g. accessing `called_once` instead of `assert_called_once`.
1303 Passing `unsafe=True` will allow access to these attributes.
1304
1305 * `wraps`: Item for the mock object to wrap. If `wraps` is not None then
1306 calling the Mock will pass the call through to the wrapped object
1307 (returning the real result). Attribute access on the mock will return a
1308 Mock object that wraps the corresponding attribute of the wrapped object
1309 (so attempting to access an attribute that doesn't exist will raise an
1310 `AttributeError`).
1311
1312 If the mock has an explicit `return_value` set then calls are not passed
1313 to the wrapped object and the `return_value` is returned instead.
1314
1315 * `name`: If the mock has a name then it will be used in the repr of the
1316 mock. This can be useful for debugging. The name is propagated to child
1317 mocks.
1318
1319 Mocks can also be called with arbitrary keyword arguments. These will be
1320 used to set attributes on the mock after it is created.
1321 """
1322

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…