我正在为我的模块创建一个测试套件,它大量使用请求库。但是,我正在尝试为特定请求模拟几个不同的返回值,但我在这样做时遇到了麻烦。这是我不起作用的代码片段:
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 解码器设置返回值。我似乎也找不到解决此问题的方法,因为我似乎也无法修补内部功能,这也可以解决此问题。有人有什么建议/想法吗?非常感谢。
原文由 the_deuce 发布,翻译遵循 CC BY-SA 4.0 许可协议
当你
mock
一个类时,每个子方法都被设置为一个新的MagicMock
这又需要进行配置。因此,在这种情况下,您需要为return_value
设置mock_post
以生成子属性, 并 实际返回一些内容,即:您可以通过查看所有内容的类型来了解这一点:
在这两种情况下,类型都是
<class 'unittest.mock.MagicMock'>