python - 模拟一个 api 响应
问题描述
背景
我有一个名为 submit_health 的函数,它负责对我们传递的一些数据进行 POST 请求。预期输出始终采用以下形式:
{
"errors": [],
"warnings": [],
"success": true
}
唯一的区别是,在某些情况下,如果我们发送无效数据"success": false
是可能的,当然“错误”和“警告”会有适当的文本说明为什么没有成功的发布请求。
代码
def submit_health(health_data):
return _post_json("health-single", health_data)
def _post_json(resource: str, data: Dict) -> PostResponse:
json_data = json.dumps(data, default=json_encoding_decimal_default)
response = request_session_with_retry().post(
f"{SITE_API_ROOT}/v3/{resource}",
auth=("", SITE_API_V3_KEY),
data=json_data,
headers={"Content-Type": "application/json", "User-Agent": USER_AGENT},
)
response.raise_for_status()
try:
return json.loads(response.text)
except Exception:
return None
问题
我正在尝试submit_health
使用 pytest 测试该功能。我不关心 API 的实现,因为代码的不同部分正在处理该测试。我只关心用预期的输出来测试它
{
"errors": [],
"warnings": [],
"success": true
}
我的问题是我将如何模拟此响应?我很想提出任何建议。我读了一些关于猴子补丁的文章,但我不太确定如何模拟响应。我很想得到一些指导。
解决方案
假设您的函数位于health.py
模块中,我将test_health.py
使用代码创建一个模块:
from unittest.mock import Mock, patch
from health import submit_health
@patch("health.request_session_with_retry")
def test_submit_health(request_mock):
response_mock = Mock(text='{"errors": [], "warnings": [], "success": true}')
request_mock.return_value.get.return_value = response_mock
result = submit_health({"foo": "bar"})
assert result == {"errors": [], "warnings": [], "success": True}
@patch
将修补后的函数作为我命名的参数传递request_mock
- 我们需要 mas 在
request_session_with_retry.get
调用时模拟将返回的内容
推荐阅读
- jenkins - 传递自己的 Jenkins 内部版本号格式
- oracle - Invoker 的 Right 函数结果缓存
- c++ - c++ 负数比较
- react-native - 调用 onsend 时不会触发 WebSocket onmessage
- javascript - 谁能解释一下 Vue/Javascript 中的“代理”或“代理”是什么意思?
- node.js - mongoDB有没有办法在特定时间后自己创建一个文档,比如一天?
- php - 使用文件夹名称保存到数据库的干预图像上传
- python - 在 Python 中使用变量更新 Kivy 标签
- angular - 在路由中传递对象作为参数返回 [Object object]
- c++ - 如何从给定的一组点计算同心多边形的最大数量?