简体   繁体   English

python 2.7-无法弄清楚如何使用模拟进行测试

[英]python 2.7 - Can't figure out how to test with mock

New to using mock. 使用模拟的新手。 on python 2.7.13. 在python 2.7.13上。

i'm building a wrapper around this library 我正在围绕这个图书馆建立一个包装
https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/sendgrid.py https://github.com/sendgrid/sendgrid-python/blob/master/sendgrid/sendgrid.py

which in turn consumes this library for any REST calls 反过来,该库会为任何REST调用占用该库
https://github.com/sendgrid/python-http-client/blob/master/python_http_client/client.py https://github.com/sendgrid/python-http-client/blob/master/python_http_client/client.py

My test code looks like 我的测试代码看起来像

class TestSendgridUtils(unittest.TestCase):
    def setUp(self):
        self.sgsu = SubUser(api_key=RANDOM_API_KEY, hippo_user=RANDOM_USER_OBJ)

    @patch('sendgrid.SendGridAPIClient')
    @patch('python_http_client.Client')
    def test_create(self, sgc_mock, http_client_mock):
        self.sgsu.create()
        expected_data = {
            'email' : self.sgsu.sg_username
        }
        print http_client_mock.call_list()
        sgc_mock.client.assert_called_with(request_body=expected_data)

I'm basically trying to mock the underlying library that makes the HTTP calls. 我基本上是在尝试模拟进行HTTP调用的基础库。 My tests are just to ensure that i'm passing the right parameters to the sendgrid module. 我的测试只是为了确保我将正确的参数传递给sendgrid模块。

Right now if i run the tests, HTTP requests are still being made which means i'm not successfully mocking the intended libraries. 现在,如果我运行测试,仍会发出HTTP请求,这意味着我没有成功模拟预期的库。

Reading the mock documentation I understand that patch only works where i instantiate the underlying class. 阅读模拟文档后,我了解到该修补程序仅在实例化基础类的地方有效。 But this would have to be in setUp() which would mean my mocks wouldn't be available in my test cases? 但这必须在setUp()中,这意味着我的模拟在测试用例中不可用?

As a result I can't figure out what is the best practice around how I use mock in this instance. 结果,我不知道在这种情况下如何使用模拟的最佳实践是什么。 I also can't figure out if i can just mock the 'sendgrid.SendGridAPIClient' or if i need to mock 'python_http_client.Client' 我也无法弄清楚我是否可以模拟'sendgrid.SendGridAPIClient'或是否需要模拟'python_http_client.Client'

You should patch objects in the module where you're using them (eg in app.py ), not where they are defined. 您应该在使用对象的模块中(例如,在app.pypatch对象,而不是在定义它们的位置。
So your calls to patch should look like this: 因此,您打patch的电话应如下所示:

@patch('app.sendgrid.SendGridAPIClient')

not: 不:

@patch('sendgrid.SendGridAPIClient')

Also your order of mocks is mismatched: 您的模拟顺序也不匹配:

@patch('sendgrid.SendGridAPIClient')
@patch('python_http_client.Client')
def test_create(self, sgc_mock, http_client_mock):

You have to either switch calls to patch or switch method arguments, because now @patch('sendgrid.SendGridAPIClient') corresponds to http_client_mock and @patch('python_http_client.Client') to sgc_mock . 您必须将调用切换到patch或切换方法参数,因为现在@patch('sendgrid.SendGridAPIClient')对应于http_client_mock@patch('python_http_client.Client')sgc_mock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM