How to get the call count using Mock @patch?

Question:

I am writing a unit test for some library we are working on. This library makes use of requests.post() to perform POST HTTP requests to an external server.

Inside my UT, I obviously don’t want to contact the real server but to mock the response.

To do that, I wrote a function that goes like:

def mocked_post(url, headers, data, **kwargs):
    response = Mock()

    # Some logic, irrelevant here.

    return response

And I patched this function around my unit test class:

@patch('mylib.requests.post', mocked_post)
class MyTest(TestCase):

    def test_foo(self):
        # Some test logic

This is working perfectly.

Now I’d like to get the number of calls to my mocked function. I tried mocked_post.call_count but that doesn’t exist. I tried to find this property on a lot of different objects (including mylib.requests.post) but no luck so far.

How can I access the call_count for this mocked function ?

Asked By: ereOn

||

Answers:

I’d not use mocked_post as the new argument here. I’d set up the side_effect attribute of a fresh Mock instead:

@patch('mylib.requests.post')
class MyTest(TestCase):

    def test_foo(self, post_mock):
        post_mock.side_effect = mocked_post

        # Some test logic

        self.assertEqual(post_mock.call_count, 3)

Now you have the Mock object that patch generates for you as an argument to all your test methods, and you can thus test how many times that mock was called.

You should be able to set the side_effect attribute in the decorator as well, to apply to all tests:

@patch('mylib.requests.post', side_effect=mocked_post)
class MyTest(TestCase):

    def test_foo(self, post_mock):
        # Some test logic

        self.assertEqual(post_mock.call_count, 3)

You’ll still have trouble accessing the returned response object, however; you may want to return mock.DEFAULT from mocked_post instead of creating one in the function, so that you can then use post_mock.return_value to make further assertions on the returned object.

Answered By: Martijn Pieters
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.