Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python mock patch instance method and check call arguments

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?

like image 419
darksky Avatar asked May 27 '15 16:05

darksky


1 Answers

Essentially there are two ways to play around self reference for autospec=True patches.

  1. Use mock.ANY to ignore the first argument
  2. Patch object by patch.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)
like image 89
Michele d'Amico Avatar answered Sep 19 '22 02:09

Michele d'Amico