[英]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_code
和json()
。
在这里写下来,认为有人可能会发现它有用。
当你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.