python中的模拟
我试图模仿下面的方法,但没能做到 数据_test.py:python中的模拟,python,Python,我试图模仿下面的方法,但没能做到 数据_test.py: def test1: res = get_url(url) status_code = res.status_code if status_code == http.HTTPStatus.OK: response_data = res.json() def get_url(url): resp = requests.request('GET', url, header=self.hea
def test1:
res = get_url(url)
status_code = res.status_code
if status_code == http.HTTPStatus.OK:
response_data = res.json()
def get_url(url):
resp = requests.request('GET', url, header=self.header)
return resp
test-data_test.py(模拟文件)
但是,上面的代码给出了错误,因为“res”的值为None。有人能帮我整理一下这个代码吗?我需要模拟以获得正确的HTTP状态OK resp,并返回resp.json值。
get\u url
实际上不会返回任何内容。因此,非常确定kwarg在请求.请求()中是头而不是头。此外,如果您总是使用GET,您可以使用requests.GET()
来代替。GET\u url返回resp(我编辑了上面的示例)还请注意,我已经更改了代码,实际上GET\u url不会发布。还有GET。为什么要模拟请求。GET
?那么解决方案是什么?
@mock.patch("data_test.get_url")
@mock.patch("requests.get")
def test_01_check_database(self, mock_get, gurl):
gurl.return_value.json.return_value = {'status':http.HTTPStatus.OK,'data':{'data':'data'}}