Mocking Magic Methods

Mock supports mocking magic methods. This allows mock objects to replace containers or other objects that implement Python protocols.

Because magic methods are looked up differently from normal methods [1], this support has been specially implemented. This means that only specific magic methods are supported. The supported list includes almost all of them. If there are any missing that you need please let us know!

You mock magic methods by setting the method you are interested in to a function or a mock instance. If you are using a function then it must take self as the first argument [2].

>>> from mock import Mock
>>> def __str__(self):
...     return 'fooble'
...
>>> mock = Mock()
>>> mock.__str__ = __str__
>>> str(mock)
'fooble'

>>> from mock import Mock
>>> mock = Mock()
>>> mock.__str__ = Mock()
>>> mock.__str__.return_value = 'fooble'
>>> str(mock)
'fooble'

>>> from mock import Mock
>>> mock = Mock()
>>> mock.__iter__ = Mock(return_value=iter([]))
>>> list(mock)
[]

One use case for this is for mocking objects used as context managers in a with statement:

>>> from mock import Mock
>>> mock = Mock()
>>> mock.__enter__ = Mock()
>>> mock.__exit__ = Mock()
>>> mock.__exit__.return_value = False
>>> with mock:
...     pass
...
>>> mock.__enter__.assert_called_with()
>>> mock.__exit__.assert_called_with(None, None, None)

Calls to magic methods do not (yet) appear in mock.Mock.method_calls. This may change in a future release.

The full list of supported magic methods is:

  • __hash__, __repr__, __str__, __dir__, __format__ and __subclasses__
  • Comparisons: __cmp__, __lt__, __gt__, __le__, __ge__, __eq__ and __ne__
  • Container methods: __getitem__, __setitem__, __delitem__, __contains__, __len__, __iter__, __getslice__, __setslice__, __reversed__ and __missing__
  • Context manager: __enter__ and __exit__
  • Unary numeric methods: __neg__, __pos__ and __invert__
  • The numeric methods (including right hand and in-place variants): __add__, __sub__, __mul__, __div__, __truediv__, __floordiv__, __mod__, __divmod__, __lshift__, __rshift__, __and__, __xor__, __or__, and __pow__
  • Numeric conversion methods: __complex__, __int__, __float__, __index__ and __coerce__
  • Descriptor methods: __get__, __set__ and __delete__

The following methods are supported in Python 2 but don’t exist in Python 3:

  • __unicode__, __long__, __oct__, __hex__ and __nonzero__

The following methods are supported in Python 3 but don’t exist in Python 2:

  • __bool__ and __next__

The following methods exist but are not supported as they either can’t be dynamically set or can cause problems:

  • __prepare__, __instancecheck__, __subclasscheck__, __del__

Magic Mock

class MagicMock(*args, **kw)
MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself.

The magic methods are setup with Mock objects, so you can configure them and use them in the usual way:

>>> from mock import MagicMock
>>> mock = MagicMock()
>>> mock[3] = 'fish'
>>> mock.__setitem__.assert_called_with(3, 'fish')
>>> mock.__getitem__.return_value = 'result'
>>> mock[2]
'result'

By default many of the protocol methods are required to return objects of a specific type. These methods are preconfigured with a default return value, so that they can be used without you having to do anything if you aren’t interested in the return value. You can still set the return value manually if you want to change the default.

Methods and their defaults:

  • __int__ : 0
  • __contains__ : False
  • __len__ :0
  • __iter__ : iter([])
  • __exit__ : False
  • __complex__ : 0j
  • __float__ : 0.0
  • __bool__ : True
  • __nonzero__ : True
  • __oct__ : ‘0’
  • __hex__ : ‘0x0’
  • __long__ : long(0)
  • __index__ : 0
  • __hash__ : default hash for the mock
  • __repr__ : default repr for the mock
  • __str__ : default str for the mock
  • __unicode__ : default unicode for the mock

For example:

>>> from mock import MagicMock
>>> mock = MagicMock()
>>> int(mock)
0
>>> len(mock)
0
>>> hex(mock)
'0x0'
>>> list(mock)
[]
>>> object() in mock
False

MagicMock has all of the supported magic methods configured except for some of the obscure and obsolete ones. You can still set these up if you want.

Magic methods that are supported but not setup by default in MagicMock are:

  • __cmp__
  • __getslice__ and __setslice__
  • __coerce__
  • __subclasses__
  • __dir__
  • __format__
  • __get__, __set__ and __delete__
  • __reversed__ and __missing__

[1]Magic methods should be looked up on the class rather than the instance. Different versions of Python are inconsistent about applying this rule. The supported protocol methods should work with all supported versions of Python.
[2]The function is basically hooked up to the class, but each Mock instance is kept isolated from the others.

Table Of Contents

Previous topic

Sentinel

Next topic

mocksignature

This Page