Sai*_*Sai 1 python httpexception pytest
**main.py:**
def bucket_exists(bucket_name):
try:
something()
except ClientError as error:
error_code = int(error.response['Error']['Code'])
if error_code == 403:
raise HTTPException(status_code=403, detail=f"Private Bucket. Forbidden Access!")
elif error_code == 404:
raise HTTPException(status_code=404, detail=f"Bucket Does Not Exist!")
return flag
**test_main.py:**
def test_bucket_exists(mock_s3_bucket):
resp = fetch_data.bucket_exists('abc123')
assert resp == {"detail":"Bucket Does Not Exist!"}
Run Code Online (Sandbox Code Playgroud)
我的测试失败并出现错误:fastapi.exceptions.HTTPException
请帮助处理 pytest 中已知的 httpException。我读过其他帖子,但与我的测试用例无关
尝试将异常捕获到上下文pytest.raises中exc_info,然后value对其属性进行断言,该属性包含引发的异常对象:
def test_bucket_exists(mock_s3_bucket):
with pytest.raises(HTTPException) as exc_info:
fetch_data.bucket_exists('abc123')
assert isinstance(exc_info.value, HTTPException)
assert exc_info.value.status_code == 404
assert exc_info.value.detail == "Bucket Does Not Exist!"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2573 次 |
| 最近记录: |