[TIP] Error with Checking Calls to Multiple Mocks

John Papanastasiou johnnypapps at gmail.com
Mon May 20 03:52:36 PDT 2013


I believe I have found a problem with mock.call_args_list.  My
understanding is that for a given mock, if the function you are testing
makes multiple calls to a function on that mock, the call_args_list is set
to a list of the successive argument lists for each call.

The problem I have is that the call_args_list for one mock is being
replaced with the call_args_list for a second mock that is being used in
the same function under test.

To demonstrate this, I have written a script which you can just run.

I have an event object which has a property which may be set to a list of
extra event objects.  Event objects have a property called handler which
itself has a function trigger which called with a context (a dict with a
key 'event' set to the event instance).

The function i am trying to test triggers the handler on the top level
event, then goes through each event in the list triggering their handlers.
 To test this function, I am making assertions about the calls to the
respective trigger functions.

The attached code demonstrates that the call_args_list for the top_level
event (the one being called first in the function) is set to the
call_args_list for the extra event (the one being called second).

The TestCase includes two unit tests which test the calls to the top_level
event trigger and the nested event trigger independently and these pass.
 The third unit test, test_top_level_and_extra prints out some information
about the mocks involved and the call_args_lists.  This output demonstrates
the error I am seeing.  I've pasted a sample output below.

Is this a defect in the mock.call_args_list or have I misunderstood the

Any help you can give is greatly appreciated.



Sample Output:

$ ./nested_calls.py
Mock: top_level: <mock.MagicMock object at 0x100567590>
Call Args, top_level.handler.trigger: [(({'event': <mock.Mock object at
0x10059a950>},), {})]
Mock: nested: <mock.Mock object at 0x10059a950>
Call Args, nested.handler.trigger: [(({'event': <mock.Mock object at
0x10059a950>},), {})]

FAIL: test_top_level_and_extra (__main__.TestNestedCalls)
Traceback (most recent call last):
  File "./nested_calls.py", line 53, in test_top_level_and_extra
AssertionError: Expected: (({'event': <mock.MagicMock object at
0x100567590>},), {})
Called with: (({'event': <mock.Mock object at 0x10059a950>},), {})

Ran 3 tests in 0.059s

FAILED (failures=1)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130520/29877451/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: nested_calls.py
Type: application/octet-stream
Size: 1608 bytes
Desc: not available
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20130520/29877451/attachment.obj>

More information about the testing-in-python mailing list