Edit: This may be the same issue as described in this Python bug: https://bugs.python.org/issue26752. But that bug has been sitting for a year, so I'm still interested in people's opinions here.
I'm not sure if this is a bug in unittest.mock
or I just misunderstand something.
Here's the code - you can save it as test.py
from unittest.mock import patch, call
class Foo:
def __init__(self):
pass
def my_method(self, value):
pass
def test_foo():
with patch('test.Foo', autospec=True) as MockFoo:
m = Foo()
m.my_method(123)
MockFoo.assert_has_calls([call(), call().my_method(123)])
I run this test like so:
$ py.test test.py
And I get this failure:
...
E AssertionError: Calls not found.
E Expected: [call(), call().my_method(123)]
E Actual: [call(), call().my_method(123)]
The Question: Is this correct behavior? It seems buggy to me. The list of calls matches up exactly, so what gives?
Interestingly, if I remove the value
parameter of my_method
and also the 123
inputs in the test, then everything passes!
What am I missing, here?
Version info:
$ py.test --version
This is pytest version 3.0.6, imported from /usr/local/lib/python3.4/site-packages/pytest.py
$python3.4 --version
Python 3.4.5
Also tried this on 3.5 in a virtualenv:
$ py.test --version
This is pytest version 3.0.6, imported from /usr/home/jwd/virtualenv/pytest-3.5/lib/python3.5/site-packages/pytest.py
$ python3.5 --version
Python 3.5.2
Foo
if you want to test it? – user3850 Feb 13 '17 at 20:24assert_has_calls
has this non-intuitive (in my opinion) behavior when args are passed. – jwd Feb 13 '17 at 21:06Foo
. That method is the one that instantiatesFoo
and callsmy_method(123)
. I just inlined it into the test here for brevity. – jwd Feb 13 '17 at 21:07MockFoo()
withFoo()
in the test. The same issue still occurs, and still magically disappears if I remove thevalue
argument. Does it seem like a valid usage now (albeit contrived)? – jwd Feb 14 '17 at 17:31