I am using Mock (http://mock.readthedocs.org/en/latest/) library with Python 2.7. I have a main function that calls a few other functions that I am trying to test.
The other functions that it calls are other instance methods (so for example, def _other_function(self, a, b)
.
I am calling my main function, and I have the other functions that it calls patched. I just added autospec=True
to the patch. When I check for call arguments however, it shows a self
argument (as expected):
python2.7> _other_function_mock.call_args_list
[call(<some.module.class.method object at 0x9acab90>, 1, 2)]
Before setting autospec=True
, it would only show that arguments that I actually passed (1 and 2). Since now the call args show a reference to self
, I can't just call mock_object.assert_any_call(1, 2)
. I will need to pick out the arguments from mock_object.call_args_list
and compare.
Is there a way to still call mock.assert_any_call
without having to pick out the arguments manually to check that the arguments passed are correct?
Or is there something better in general that I can do to patch instance methods?
Essentially there are two ways to play around self
reference for autospec=True
patches.
mock.ANY
to ignore the first argumentpatch.object
instead of patching the static method reference.Anyway 2 cannot be used in all cases sometimes you cannot have the object instance in test method context; moreover that way often make test less clear and more complicated. I always prefer to use 1 in my tests:
@patch("my_module.MyClass.my_method", autospec=True)
def test_my_test(self, mock_my_method):
my_module.MyClass().my_method(1,2)
mock_my_method.assert_any_call(mock.ANY, 1, 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With