存根boto3客户端以避免在测试中碰到真正的aws端点
stuboto的Python项目详细描述
斯塔博托
存根boto3
客户端,以避免在测试中碰到真正的aws端点。
用法
您可以使用Stuboto
实例,就好像它们是botocore.stub.Stubber
:
importboto3fromstubotoimportStubotodeftest_create_bucket():s3=boto3.client("s3")stubber=Stuboto(s3)stubber.activate()response={"Location":"my-bucket"}expected_params={"Bucket":"my-bucket","ACL":"private"}stubber.add_response("create_bucket",response,expected_params)service_response=s3.create_bucket(Bucket="my-bucket",ACL="private")assertservice_response==response
Stuboto
实例使用与原始客户机相同的所有方法进行修饰,因此您还可以使用更具可读性的存根响应:
importboto3fromstubotoimportStubotodeftest_create_bucket():s3=boto3.client("s3")stubber=Stuboto(s3)stubber.activate()response=stubber.create_bucket(Bucket="my-bucket",ACL="private").add_response(Location="my-bucket")service_response=s3.create_bucket(Bucket="my-bucket",ACL="private")assertservice_response==response
botocore.stub.Stubber
文档建议:
It should be noted, however, that while missing attributes are often considered correct, your code may not function properly if you leave them out. Therefore you should always fill in every value you see in a typical response for your particular request.
但是如果你为了编写更可读的测试而遗漏了一些值,我不会告诉任何人。
安装
pip install stuboto