Mock vs MagicMock Mock vs MagicMock python python

Mock vs MagicMock


What is the reason for plain Mock existing?

Mock's author, Michael Foord, addressed a very similar question at Pycon 2011 (31:00):

Q: Why was MagicMock made a separate thing rather than just folding the ability into the default mock object?

A: One reasonable answer is that the way MagicMock works is that it preconfigures all these protocol methods by creating new Mocks and setting them, so if every new mock created a bunch of new mocks and set those as protocol methods and then all of those protocol methods created a bunch more mocks and set them on their protocol methods, you've got infinite recursion...

What if you want accessing your mock as a container object to be an error -- you don't want that to work? If every mock has automatically got every protocol method, then it becomes much more difficult to do that. And also, MagicMock does some of this preconfiguring for you, setting return values that might not be appropriate, so I thought it would be better to have this convenience one that has everything preconfigured and available for you, but you can also take a ordinary mock object and just configure the magic methods you want to exist...

The simple answer is: just use MagicMock everywhere if that's the behavior you want.


With Mock you can mock magic methods but you have to define them. MagicMock has "default implementations of most of the magic methods.".

If you don't need to test any magic methods, Mock is adequate and doesn't bring a lot of extraneous things into your tests. If you need to test a lot of magic methods MagicMock will save you some time.


To begin with, MagicMock is a subclass of Mock.

class MagicMock(MagicMixin, Mock)

As a result, MagicMock provides everything that Mock provides and more. Rather than thinking of Mock as being a stripped down version of MagicMock, think of MagicMock as an extended version of Mock. This should address your questions about why Mock exists and what does Mock provide on top of MagicMock.

Secondly, MagicMock provides default implementations of many/most magic methods, whereas Mock doesn't. See here for more information on the magic methods provided.

Some examples of provided magic methods:

>>> int(Mock())TypeError: int() argument must be a string or a number, not 'Mock'>>> int(MagicMock())1>>> len(Mock())TypeError: object of type 'Mock' has no len()>>> len(MagicMock())0

And these which may not be as intuitive (at least not intuitive to me):

>>> with MagicMock():...     print 'hello world'...hello world>>> MagicMock()[1]<MagicMock name='mock.__getitem__()' id='4385349968'>

You can "see" the methods added to MagicMock as those methods are invoked for the first time:

>>> magic1 = MagicMock()>>> dir(magic1)['assert_any_call', 'assert_called_once_with', ...]>>> int(magic1)1>>> dir(magic1)['__int__', 'assert_any_call', 'assert_called_once_with', ...]>>> len(magic1)0>>> dir(magic1)['__int__', '__len__', 'assert_any_call', 'assert_called_once_with', ...]

So, why not use MagicMock all the time?

The question back to you is: Are you okay with the default magic method implementations? For example, is it okay for mocked_object[1] to not error? Are you okay with any unintended consequences due to the magic method implementations being already there?

If the answer to these questions is a yes, then go ahead and use MagicMock. Otherwise, stick to Mock.