PyDocs

Nav

Development Tools

create_autospec

  • create_autospec(spec, spec_set=False, instance=False, **kwargs)
Create a mock object using another object as a spec. Attributes on the
mock will use the corresponding attribute on the `spec` object as their
spec.

Functions or methods being mocked will have their arguments checked
to check that they are called with the correct signature.

If `spec_set` is True then attempting to set attributes that don't exist
on the spec object will raise an `AttributeError`.

If a class is used as a spec then the return value of the mock (the
instance of the class) will have the same spec. You can use a class as the
spec for an instance object by passing `instance=True`. The returned mock
will only be callable if instances of the mock are callable.

`create_autospec` will raise a `RuntimeError` if passed some common
misspellings of the arguments autospec and spec_set. Pass the argument
`unsafe` with the value True to disable that check.

`create_autospec` also takes arbitrary keyword arguments that are passed to
the constructor of the created mock.
1 Example
>>> from urllib import request
>>> mock_request = create_autospec(request)
>>> mock_request.Request('foo', 'bar')
<NonCallableMagicMock name='mock.Request()' spec='Request' id='...'>
from the Python docs
See Also
No see-alsos for unittest.mock/create_autospec
0 Notes
No notes for create_autospec