[TIP] Is this a bug in unittest.mock, or am I missing something?

James Cooke me at jamescooke.info
Thu Feb 16 14:37:16 PST 2017

Hi John,

This is interesting and thanks for sharing it. Running python 3.5.2, I
firstly simplified your example and got a slightly different exception:

from unittest import mock

def test_calls_match():


    Calls on a Mock match expectation


    m = mock.Mock()



    m.assert_has_calls([mock.call(), mock.call().my_method(123)])

The exception is slightly different for me:

E               AssertionError: Calls not found.

E               Expected: [call(), call().my_method(123)]

E               Actual: [call(), call.my_method(123)]

When I remove the parentheses after the second `call`, then everything
works for me, and the test passes.

    m.assert_has_calls([mock.call(), mock.call.my_method(123)])

My assumption is that MagicMock will operate like Mock and that your
MockFoo is a MagicMock, so could you try the `mock.call.my_method` on
Python 3.4?

I'm interested to hear how that works for you.



On Thu, 16 Feb 2017, at 07:06 PM, John W wrote:

> They seem to be the same types.


> I added these prints:


>         print("type of call():", type(call()))

>         print("type of call().my_method(123):",

>         type(call().my_method(123)))

>         for c in MockFoo.mock_calls:

>             print("type of mock call '{}': {}".format(c, type(c)))


> And output is:


>     type of call(): <class 'unittest.mock._Call'>

>     type of call().my_method(123): <class 'unittest.mock._Call'>

>     type of mock call 'call()': <class 'unittest.mock._Call'>

>     type of mock call 'call().my_method(123)': <class

>     'unittest.mock._Call'>



> Thanks for the suggestion though.

> I've been getting crickets from every place I've asked this
> question (:

> -John


> _______________________________________________

> testing-in-python mailing list

> testing-in-python at lists.idyll.org

> http://lists.idyll.org/listinfo/testing-in-python


James Cooke

Backend software developer

CV PDF: http://jamescooke.info/docs/james_cooke_cv.pdf

Website: http://jamescooke.info/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20170216/67b88699/attachment.htm>

More information about the testing-in-python mailing list