gpt4 book ai didi

python - 如何在 python unittest 中模拟 http 请求

转载 作者:行者123 更新时间:2023-12-02 16:03:23 25 4
gpt4 key购买 nike

第一次写单元测试。生产代码:

def get_session_token(organization_id):
endpoint = get_endpoint(organization_id)
response = requests.post(
endpoint + "/some/url/part",
data=json.dumps({
"Login": CONFIG[organization_id]["username"],
"Password": CONFIG[organization_id]["password"],
}),
headers={"Accept": "application/json"}
)

if response.status_code != 201:
log.error("filename.get_session_token(%r): couldn't auth: %r %r",
organization_id, response, response.text)
raise ValueError()

return response.json()

def member(organization_id):
session_key = get_session_token(organization_id)
(some other code...)

我需要测试成员。我有测试代码:

@patch('requests.post')
def test_member(self, mock_post):
mock_post().status_code = 201
mock_response = mock_post("some/url", data=ANY,
headers={"Accept": "application/json"})
mock_response.status_code = 201
(some other code...)

每次我运行测试时,它总是引发 ValueError()(这是一个 403 错误)

我怎样才能绕过 requests.post 并获得 201?

谢谢!

最佳答案

参见 Where to patch 。并且,您应该使用 return_valuemock_post 提供模拟返回值 - 调用模拟时返回的值。

例如

成员.py:

import requests
import json


def get_session_token(organization_id):
endpoint = 'http://localhost:3000/api'
response = requests.post(
endpoint + "/some/url/part",
data=json.dumps({
"Login": 'python',
"Password": '123456',
}),
headers={"Accept": "application/json"}
)
if response.status_code != 201:
raise ValueError()

return response.json()


def member(organization_id):
session_key = get_session_token(organization_id)
return session_key

test_member.py:

from unittest.mock import patch
import unittest
import json
from member import member


class TestMember(unittest.TestCase):
@patch('member.requests.post')
def test_member_success(self, mock_post):
mock_post.return_value.status_code = 201
mock_post.return_value.json.return_value = 'mock response'

actual = member(1)
self.assertEqual(actual, 'mock response')
mock_post.assert_called_once_with(
'http://localhost:3000/api/some/url/part',
data=json.dumps({
"Login": 'python',
"Password": '123456',
}),
headers={"Accept": "application/json"}
)

@patch('member.requests.post')
def test_member_failure(self, mock_post):
mock_post.return_value.status_code = 400
self.assertRaises(ValueError, member, 1)


if __name__ == '__main__':
unittest.main()

测试结果:

.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Name Stmts Miss Cover Missing
-------------------------------------------------------------------------
src/stackoverflow/70098351/member.py 11 2 82% 18, 23
src/stackoverflow/70098351/test_member.py 11 0 100%
-------------------------------------------------------------------------
TOTAL 22 2 91%

关于python - 如何在 python unittest 中模拟 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70098351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com