首页 > 解决方案 > 模拟一个 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
} 

我的问题是我将如何模拟此响应?我很想提出任何建议。我读了一些关于猴子补丁的文章,但我不太确定如何模拟响应。我很想得到一些指导。

标签: pythonpytest

解决方案


假设您的函数位于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}
  1. @patch将修补后的函数作为我命名的参数传递request_mock
  2. 我们需要 mas 在request_session_with_retry.get调用时模拟将返回的内容

推荐阅读