PyDocs

Nav

Development Tools

Mock

unittest.mock

class · Added in 3.5 (source)
  • Mock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, unsafe=False, **kwargs)
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 an object then a list of strings is formed by calling dir on
  the object (excluding unsupported magic attributes and methods). Accessing
  any attribute not in this list will raise an `AttributeError`.

  If `spec` is an object (rather than a list of strings) then
  `mock.__class__` returns the class of the spec object. This allows mocks
  to pass `isinstance` tests.

* `spec_set`: A stricter variant of `spec`. If used, attempting to *set*
  or get an attribute on the mock that isn't on the object passed as
  `spec_set` will raise an `AttributeError`.

* `side_effect`: A function to be called whenever the Mock is called. See
  the `side_effect` attribute. Useful for raising exceptions or
  dynamically changing return values. The function is called with the same
  arguments as the mock, and unless it returns `DEFAULT`, the return
  value of this function is used as the return value.

  If `side_effect` is an iterable then each call to the mock will return
  the next value from the iterable. If any of the members of the iterable
  are exceptions they will be raised instead of returned.

* `return_value`: The value returned when the mock is called. By default
  this is a new Mock (created on first access). See the
  `return_value` attribute.

* `unsafe`: By default, accessing any attribute whose name starts with
  *assert*, *assret*, *asert*, *aseert*, or *assrt* raises an AttributeError.
  Additionally, an AttributeError is raised when accessing
  attributes that match the name of an assertion method without the prefix
  `assert_`, e.g. accessing `called_once` instead of `assert_called_once`.
  Passing `unsafe=True` will allow access to these attributes.

* `wraps`: Item for the mock object to wrap. If `wraps` is not None then
  calling the Mock will pass the call through to the wrapped object
  (returning the real result). Attribute access on the mock will return a
  Mock object that wraps the corresponding attribute of the wrapped object
  (so attempting to access an attribute that doesn't exist will raise an
  `AttributeError`).

  If the mock has an explicit `return_value` set then calls are not passed
  to the wrapped object and the `return_value` is returned instead.

* `name`: If the mock has a name then it will be used in the repr of the
  mock. This can be useful for debugging. The name is propagated to child
  mocks.

Mocks can also be called with arbitrary keyword arguments. These will be
used to set attributes on the mock after it is created.
Members of Mock

_

__class__
Normally the __class__ attribute of an object will return its type.
__dir__
Filter the output of `dir(mock)` to only useful members.
_get_child_mock
Create the child mocks for attributes and return value.

a

assert_any_call
Assert the mock has been called with the specified arguments.
assert_called
Assert that the mock was called at least once.
assert_called_once
Assert that the mock was called only once.
assert_called_once_with
Assert that the mock was called exactly once and that call was with the specified arguments.
assert_called_with
Assert that the last call was made with the specified arguments.
assert_has_calls
Assert the mock has been called with the specified calls.
assert_not_called
Assert that the mock was never called.
attach_mock
Attach a mock as an attribute of this one, replacing its name and parent.

c

call_args
This is either None (if the mock hasn’t been called), or the arguments that the mock was last called with.
call_args_list
This is a list of all the calls made to the mock object in sequence (so the length of the list is the number of times it has been called).
call_count
An integer telling you how many times the mock object has been called:
called
A boolean representing whether or not the mock object has been called:
configure_mock
Set attributes on the mock through keyword arguments.

m

method_calls
As well as tracking calls to themselves, mocks also track calls to methods and attributes, and their methods and attributes:
mock_add_spec
Add a spec to a mock.
mock_calls
mock_calls records all calls to the mock object, its methods, magic methods and return value mocks.

r

reset_mock
Restore the mock object to its initial state.
return_value
The value to be returned when the mock is called.

s

side_effect
This can either be a function to be called when the mock is called, an iterable or an exception (class or instance) to be raised.
1 Example
>>> from unittest.mock import Mock
>>> mock = Mock(side_effect=KeyError('foo'))
>>> mock()
Traceback (most recent call last):
 ...
KeyError: 'foo'
from the Python docs
See Also
No see-alsos for unittest.mock/Mock
0 Notes
No notes for Mock