python - 在 requests_mock 中模拟 response.elapsed
问题描述
我在我的单元测试中使用 requests_mock 并且想模拟 response.elapsed 属性但没有找到正确的方法来做到这一点。刚刚找到了将睡眠添加到文本回调的解决方法。
with requests_mock.mock() as m:
def text_callback_with_delay(request, context):
time.sleep(2)
return "{}"
m.get(GET_REQUEST_URL, text=text_callback_with_delay)
有没有更好的方法来模拟response.elapsed使用requests_mock?
解决方案
上面给出的例子 response.elapsed 比 2 秒多一点。所以我只是使用unittest.mock.patch来修补requests.request
mock_response = Mock(spec=requests.Response)
mock_response.elapsed = datetime.timedelta(seconds=2.0)
with patch("requests.request", return_value=mock_response):
my_code_here
推荐阅读
- excel - 比较 2 列范围
- python - 如何将值列表添加到熊猫列
- powerbi - 在 power bi 服务器中找不到发布按钮
- azure-active-directory - 本地 AD 中的用户不会以访客身份同步到 Azure AD
- java-time - 通过 Spring Boot JPA、PostgreSQL 加载后 ZonedDateTime 值不正确
- python - 在张量流中获得独特面具的优雅方式?
- python - Tensorflow 2.0 GPU 不显示输出(可能崩溃)
- javascript - 如何控制表单文本输入字段中的字体
- r - 拟合多项式模型时出现 runjags 错误:“部分观察到部分缺失”
- spacy - 重新训练自定义 NER 会丢失有关先前标签的信息