繁体   English   中英

模拟requests.post和requests.json解码器python

[英]Mocking requests.post and requests.json decoder python

我正在为我的模块创建一个测试套件,它使用了很多请求库。 但是,我正在尝试为特定请求模拟几个不同的返回值,而我却遇到了麻烦。 这是我的代码片段不起作用:

class MyTests(unittest.TestCase):

    @patch('mypackage.mymodule.requests.post') 
    def test_change_nested_dict_function(self, mock_post):
        mock_post.return_value.status_code = 200
        mock_post.return_value.json = nested_dictionary
        modified_dict = mymodule.change_nested_dict()
        self.assertEqual(modified_dict['key1']['key2'][0]['key3'], 'replaced_value')

我试图模拟的功能:

import requests

def change_nested_dict():
    uri = 'http://this_is_the_endpoint/I/am/hitting'
    payload = {'param1': 'foo', 'param2': 'bar'}
    r = requests.post(uri, params=payload)

    # This function checks to make sure the response is giving the 
    # correct status code, hence why I need to mock the status code above
    raise_error_if_bad_status_code(r)

    dict_to_be_changed = r.json()

    def _internal_fxn_to_change_nested_value(dict):
        ''' This goes through the dict and finds the correct key to change the value. 
            This is the actual function I am trying to test above'''
        return changed_dict


    modified_dict = _internal_fxn_to_change_nested_value(dict_to_be_changed)

    return modified_dict

我知道这样做的一个简单方法是没有嵌套函数,但我只是向您显示整个函数代码的一部分。 相信我,嵌套函数是必要的,我真的不想改变它的那一部分。

我的问题是,我不明白如何模拟requests.post然后设置状态代码和内部json解码器的返回值。 我也似乎找不到解决这个问题的方法,因为我似乎无法修补内部函数,这也可以解决这个问题。 有没有人有任何建议/想法? 谢谢一堆。

我碰到了这里,虽然我同意可能使用专用库是一个更好的解决方案,但我最终做了以下工作

from mock import patch, Mock

@patch('requests.post')
def test_something_awesome(mocked_post):
    mocked_post.return_value = Mock(status_code=201, json=lambda : {"data": {"id": "test"}})

这对我来说既可以在进行单元测试时获得接收器端的status_codejson()

在这里写下来,认为有人可能会发现它有用。

当你mock一个类时,每个子方法都被设置为一个新的MagicMock ,而后者又需要进行配置。 因此,在这种情况下,您需要为mock_post设置return_value以使子属性生成, 并且实际返回一个东西,即:

mock_post.return_value.status_code.return_value = 200
mock_post.return_value.json.return_value = nested_dictionary

你可以通过查看所有内容的类型来看到这一点:

print(type(mock_post))
print(type(mock_post.json))

在这两种情况下,类型都是<class 'unittest.mock.MagicMock'>

您可能最好查看一些专门的库以进行请求测试:

回复

请求-模拟

请求测试

它们提供了在单元测试中模拟响应的简洁方法。

暂无
暂无

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

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