首页 > 解决方案 > Python3 模拟响应对象

问题描述

我有一个函数,我正在调用 API 来发布获取资源。由于我的函数没有返回任何内容,因此很难为失败场景编写单元测试。这里我想强制request.get()返回不同的 HTTP 状态码。

无论如何要模拟我的函数以返回所需的状态代码?

foo.py

def getData():
    response = requests.get(run_task_status_url, headers=iics_job_header)
    logging.debug(f"Activity Monitor API response: {response.json()}")

    if 200 == response.status_code:
        print("success")
    else 401  == response.status_code:
        print("401")

标签: pythonpython-3.x

解决方案


推荐阅读