Moto是一个允许你轻松模拟出基于AWS基础设施的测试的库。
如果你有以下的Python代码想测试:
importboto3classMyModel(object):def__init__(self,name,value):self.name=nameself.value=valuedefsave(self):s3=boto3.client('s3',region_name='us-east-1')s3.put_object(Bucket='mybucket',Key=self.name,Body=self.value)用Moto来测试:
importboto3frommotoimportmock_s3frommymoduleimportMyModel@mock_s3deftest_my_model_save():conn=boto3.resource('s3',region_name='us-east-1')#WeneedtocreatethebucketsincethisisallinMoto's'virtual'AWSaccountconn.create_bucket(Bucket='mybucket')model_instance=MyModel('steve','isawesome')model_instance.save()body=conn.Object('mybucket','steve').get()['Body'].read().decode("utf-8")assertbody=='isawesome'
评论