Python 如何使用 Mock @patch 获取呼叫计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21982964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:01:45  来源:igfitidea点击:

How to get the call count using Mock @patch?

pythonunit-testingmocking

提问by ereOn

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.

我正在为我们正在开发的某个库编写单元测试。该库用于对requests.post()外部服务器执行 POST HTTP 请求。

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

在我的 UT 中,我显然不想联系真正的服务器,而是要模拟响应。

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_countbut 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.

现在我想获得对我的模拟函数的调用次数。我试过了,mocked_post.call_count但那不存在。我试图在许多不同的对象(包括mylib.requests.post)上找到这个属性,但到目前为止没有运气。

How can I access the call_countfor this mocked function ?

我如何访问call_count这个模拟函数?

采纳答案by Martijn Pieters

I'd not use mocked_postas the newargument here. I'd set up the side_effectattributeof a fresh Mockinstead:

我不会mocked_postnew这里用作论点。我会设置一个新鲜的side_effect属性Mock

@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 Mockobject that patchgenerates for you as an argument to all your test methods, and you can thus test how many times that mock was called.

现在您有了为您生成的Mock对象patch作为所有测试方法的参数,因此您可以测试调用该模拟的次数。

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

您也应该能够side_effect在装饰器中设置该属性,以应用于所有测试:

@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 responseobject, however; you may want to return mock.DEFAULTfrom mocked_postinstead of creating one in the function, so that you can then use post_mock.return_valueto make further assertions on the returned object.

但是,您仍然无法访问返回的response对象;您可能希望返回mock.DEFAULTfrommocked_post而不是在函数中创建一个,以便您可以使用post_mock.return_value对返回的对象进行进一步的断言。