我正在尝试测试以下代码:
伟大的报告.py
from retry import retry
@retry((ReportNotReadyException), tries=3, delay=10, backoff=3)
def get_link(self):
report_link = _get_report_link_from_3rd_party(params)
if report_link:
return report_link
else:
stats.count("report_not_ready", 1)
raise ReportNotReadyException
我有模拟 _get_report_link_from_3rd_party 的测试函数,它可以测试所有内容,但我不希望此函数在我运行测试期间实际暂停执行..
@mock.patch('repo.great_report._get_report_link_from_3rd_party', return_value=None)
test_get_link_raises_exception(self, mock_get_report_link):
self.assertRaises(ReportNotReadyException, get_link)
我尝试模拟重试参数,但遇到了 get_link 不断重试的问题,这导致构建时间过长,而不仅仅是引发异常并继续。如何在我的测试中模拟 @retry 调用的参数?
最佳答案
如提示here , 一种防止实际休眠的简单方法是修补 time.sleep
函数。这是为我做的代码:
@patch('time.sleep', side_effect = lambda _: None)
关于python - 如何模拟python中导入的pypi库使用的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29996592/