gpt4 book ai didi

python - 如何在单元测试中模拟 ParameterNotFound boto3 异常?

转载 作者:行者123 更新时间:2023-12-05 01:40:01 26 4
gpt4 key购买 nike

我想测试一些错误处理逻辑,所以我想在我的单元测试中模拟一个特定的异常类型。我正在模拟对 boto3 的调用,但我想让该模拟引发 ParameterNotFound 异常。我正在测试的代码 follows this pattern :

boto3_client = boto3.client('ssm')
try:
temp_var = boto3_client.get_parameter(Name="Something not found")['Parameter']['Value']
except boto3_client.exceptions.ParameterNotFound:
... [logic I want to test]

我已经创建了一个单元测试模拟,但我不知道如何让它引发异常作为这个 ParameterNotFound 异常。我尝试了以下方法,但它不起作用,因为它在评估 except 子句时得到“异常必须从基类派生”:

@patch('patching_config.boto3.client')
def test_sample(self, mock_boto3_client):
mock_boto3_client.return_value = mock_boto3_client

def get_parameter_side_effect(**kwargs):
raise boto3.client.exceptions.ParameterNotFound()

mock_boto3_client.get_parameter.side_effect = get_parameter_side_effect

如何在单元测试中模拟 ParameterNotFound boto3 异常?

最佳答案

我认为问题出在我对 boto3 如何引发异常的误解。我在这里找到了解释:https://github.com/boto/boto3/issues/1262在“ClientError 的结构”下

Structure of a ClientError

Within ClientError (but not BotoCoreError), there will be anoperation_name attribute (should be a str) and a response attribute(should be a dict). The response attribute should have the followingform (example from a malformed ec2.DescribeImages call):

还有这里:https://codeday.me/en/qa/20190306/12210.html

{
"Error": {
"Code": "InvalidParameterValue",
"Message": "The filter 'asdfasdf' is invalid"
},
"ResponseMetadata": {
"RequestId": "aaaabbbb-cccc-dddd-eeee-ffff00001111",
"HTTPStatusCode": 400,
"HTTPHeaders": {
"transfer-encoding": "chunked",
"date": "Fri, 01 Jan 2100 00:00:00 GMT",
"connection": "close",
"server": "AmazonEC2"
},
"RetryAttempts": 0
}
}

听起来异常是作为具有 ParameterNotFound 错误代码的 ClientError 抛出的,所以我需要将其更改为

from botocore.exceptions import ClientError

然后

except ClientError as e:

在模拟中,我需要引发一个 ClientError 而不是将 ParameterNotFound 作为代码:

raise botocore.exceptions.ClientError({"Error": {"Code": "ParameterNotFound",
"Message": "Parameter was not found"}},
'get_parameter')

关于python - 如何在单元测试中模拟 ParameterNotFound boto3 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57227216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com