Moto 测试没有引发适当的异常
Moto testing not raising proper exception
我想测试以下功能:
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
在 except
子句中,异常被静音且未传播,因此您需要重新引发它:
except botocore.exceptions.ClientError as error:
print(error.response['Error']['Code'])
raise
刚刚捕获的 raise
re-raises 异常。
PS。无耻外挂:我是20年前请Guido加bareraise
的人之一! :-)
我想测试以下功能:
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
在 except
子句中,异常被静音且未传播,因此您需要重新引发它:
except botocore.exceptions.ClientError as error:
print(error.response['Error']['Code'])
raise
刚刚捕获的 raise
re-raises 异常。
PS。无耻外挂:我是20年前请Guido加bareraise
的人之一! :-)