python - 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")
解决方案
推荐阅读
- php - 计算整个类别的销售额
- pentaho-spoon - 在 PDI 中创建无块的 JSON 结构
- python - 删除带有空格或“-”的单词Python
- powershell - 用于显示注册表项的高级审核设置的 Windows 命令
- vba - 使用 VBA 进行网页抓取
- reactjs - 在 React 中集成来自 Metamask 的 web3
- java - 如何仅将网站页面发送到默认浏览器并在 webview 中加载其余网站页面?
- c++ - Visual Studio 2013 + Boost 库构建和链接问题
- mobile - 不在视图中时,数据表会在移动设备或平板电脑上重新呈现
- mysql - 多对多表中的级联删除