【发布时间】:2021-06-02 12:52:14
【问题描述】:
在 python 中编写了一些单元测试,已将其简化为一些函数以专注于该问题
所以2个函数是这个
def function_to_throw_http_error():
logger.info("function_to_throw_http_error")
def function_to_catch_http_error():
try:
function_to_throw_http_error()
except HTTPError as http_err:
logger.error(f"HTTP error occurred external service: {http_err}")
return {"statusCode": http_err.response.status_code, "body": http_err.response.reason}
测试是这样的
@patch(
"functions.my_file.function_to_throw_http_error", Mock(side_effect=HTTPError(Mock(status_code=404), 'error')),
)
def test_catching_http_error_reposnse(self):
response = function_to_catch_http_error()
self.assertTrue('error' in str(response))
但是当我运行测试时出现以下错误
> return {"statusCode": http_err.response.status_code, "body": http_err.response.reason}
E AttributeError: 'NoneType' object has no attribute 'status_code'
【问题讨论】:
标签: python unit-testing error-handling mocking pytest