[TIP] Is this a bug in unittest.mock, or am I missing something?
jwdevel at gmail.com
Tue Feb 14 09:57:29 PST 2017
I encountered my problem in Python3.4's unittest.mock, but I imagine
people on this list might be able to help (is there a better place?).
I made a SO post about this, too, but haven't seen much movement
Basically, `assert_has_calls` seems to be behaving in a strange way,
or else I am misunderstanding something - which is entirely possible,
as I'm new to unittest.mock.
I have some fairly small code that repro's the issue:
$ cat test.py
from unittest.mock import patch, call
def my_method(self, value):
with patch('test.Foo', autospec=True) as MockFoo:
# Note: in the real code, this usage of Foo is off in some
# Just putting it here for compactness.
m = Foo()
$ py.test test.py
... <snip long output> ...
E AssertionError: Calls not found.
E Expected: [call(), call().my_method(123)]
E Actual: [call(), call().my_method(123)]
The list of calls seems to match exactly, yet the assert fails.
What's going on, here?
Oddly, if I remove the 'value' parameter of my_method (and the
corresponding 123 inputs), it works fine.
Also, if I do it this way instead, everything works:
r = MockFoo.return_value
That makes sense to me, but I don't understand why it would be
different from the original case.
More information about the testing-in-python