我有以下要测试的功能:
def download(self):
s3 = boto3.client('s3')
try:
with open(self.flow_cells +'.tar', 'wb') as data:
s3.download_fileobj(
self.source_s3_bucket,
self.source_key,
data
)
return True
except botocore.exceptions.ClientError as error:
print(error.response['Error']['Code'])
我正在使用 pytest 来测试带有 moto 的代码。除了这个之外,所有其他测试和 botocore 异常都会被标记。我在标准中捕获它正在获取异常函数并打印正确的代码,但 moto 没有将其标记为异常
这是我的测试代码。
def test_download(parse_args, file_test):
with moto.mock_s3():
s3 = boto3.resource('s3')
s3.create_bucket(Bucket=parse_args.glacier_s3_bucket, CreateBucketConfiguration={
'LocationConstraint': 'us-east-1'
})
s3.create_bucket(Bucket=parse_args.output_s3_bucket, CreateBucketConfiguration={
'LocationConstraint': 'us-east-1'
})
bucket_version = s3.BucketVersioning(parse_args.glacier_s3_bucket)
bucket_version.enable()
s3.Object(parse_args.glacier_s3_bucket, 'flowcells/flowcell-testing.tar').put\
(Body=open(file_test, 'rb'))
glacier_client = GlacierRestoreClient(parse_args)
assert glacier_client.download() is True
s3.Object(glacier_client.source_s3_bucket, glacier_client.source_key).delete()
with pytest.raises(Exception) as error:
glacier_client.download()
assert 'Error' in error