我正在尝试模拟boto3 s3客户端对象中的单个方法,以抛出异常。但我需要所有其他方法才能让这个类正常工作。
这样我就可以在执行upload_part_copy时出现错误时测试单个异常测试
第一次尝试
import boto3
from mock import patch
with patch('botocore.client.S3.upload_part_copy', side_effect=Exception('Error Uploading')) as mock:
client = boto3.client('s3')
# Should return actual result
o = client.get_object(Bucket='my-bucket', Key='my-key')
# Should return mocked exception
e = client.upload_part_copy()
但是,这会产生以下错误:
ImportError: No module named S3
第二次尝试
在查看了botocore.client.py源代码之后,我发现它做了一些聪明的事情,而upload_part_copy
方法并不存在。我发现它似乎调用的是BaseClient._make_api_call
,所以我试着模拟一下
import boto3
from mock import patch
with patch('botocore.client.BaseClient._make_api_call', side_effect=Exception('Error Uploading')) as mock:
client = boto3.client('s3')
# Should return actual result
o = client.get_object(Bucket='my-bucket', Key='my-key')
# Should return mocked exception
e = client.upload_part_copy()
这抛出了一个异常...但是在我想要避免的get_object
上。
关于为什么我只能在upload_part_copy
方法上抛出异常,有什么想法吗?
发布于 2016-05-11 00:14:41
当我在这里发帖时,我设法想出了一个解决方案。在这里,希望它能有所帮助:)
import botocore
from botocore.exceptions import ClientError
from mock import patch
import boto3
orig = botocore.client.BaseClient._make_api_call
def mock_make_api_call(self, operation_name, kwarg):
if operation_name == 'UploadPartCopy':
parsed_response = {'Error': {'Code': '500', 'Message': 'Error Uploading'}}
raise ClientError(parsed_response, operation_name)
return orig(self, operation_name, kwarg)
with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call):
client = boto3.client('s3')
# Should return actual result
o = client.get_object(Bucket='my-bucket', Key='my-key')
# Should return mocked exception
e = client.upload_part_copy()
使用botocore.stub.Stubber类的Jordan Philips also posted a great solution。虽然这是一个更简洁的解决方案,但我不能模拟特定的操作。
发布于 2016-05-11 00:29:31
Botocore有一个客户端存根,您可以将其用于此目的:docs。
下面是一个将错误放入的示例:
import boto3
from botocore.stub import Stubber
client = boto3.client('s3')
stubber = Stubber(client)
stubber.add_client_error('upload_part_copy')
stubber.activate()
# Will raise a ClientError
client.upload_part_copy()
下面是一个放入正常响应的示例。此外,现在可以在上下文中使用存根。需要注意的是,存根程序将尽可能地验证您提供的响应是否与服务实际返回的内容相匹配。这并不完美,但它将保护您不会插入完全无意义的响应。
import boto3
from botocore.stub import Stubber
client = boto3.client('s3')
stubber = Stubber(client)
list_buckets_response = {
"Owner": {
"DisplayName": "name",
"ID": "EXAMPLE123"
},
"Buckets": [{
"CreationDate": "2016-05-25T16:55:48.000Z",
"Name": "foo"
}]
}
expected_params = {}
stubber.add_response('list_buckets', list_buckets_response, expected_params)
with stubber:
response = client.list_buckets()
assert response == list_buckets_response
发布于 2016-09-18 10:51:38
下面是一个简单的python单元测试示例,可用于伪造client = boto3.client('ec2')调用...
import boto3
class MyAWSModule():
def __init__(self):
client = boto3.client('ec2')
tags = client.describe_tags(DryRun=False)
class TestMyAWSModule(unittest.TestCase):
@mock.patch("boto3.client.get_tags")
@mock.patch("boto3.client")
def test_open_file_with_existing_file(self, mock_boto_client, mock_describe_tags):
mock_describe_tags.return_value = mock_get_tags_response
my_aws_module = MyAWSModule()
mock_boto_client.assert_call_once('ec2')
mock_describe_tags.assert_call_once_with(DryRun=False)
mock_get_tags_response = {
'Tags': [
{
'ResourceId': 'string',
'ResourceType': 'customer-gateway',
'Key': 'string',
'Value': 'string'
},
],
'NextToken': 'string'
}
希望这能有所帮助。
https://stackoverflow.com/questions/37143597
复制相似问题