本地AWS云堆栈的Python测试工具
boto3-fixtures的Python项目详细描述
boto3固定装置
boto3 fixtures为您的本地AWS云堆栈提供测试夹具。在
测试涉及云基础设施的软件并不一定很难!boto3-fixtures
为模拟的AWS基础设施的安装和拆卸提供了一个非常简单的框架。与moto或localstack等项目结合使用。在
支持
- 运动
- SQS公司
- 社交网站
- S3
- 兰姆达
- 发电机B
如果您希望看到对特定AWS服务的支持,请提交PR或问题!
入门
这个库提供了一个上下文修饰符boto3_fixtures.Service
,它将设置和删除AWS服务。在
importboto3_fixtureswithboto3_fixtures.Service("sqs",queues=["my-queue"])assvc:# Queues existforqueueinsvc.state["queues"]:print(f"{queue.name} - {queue.arn} - {queue.url}")# Queues destroyed
将其与您选择的本地测试堆栈(moto、localstack)结合使用。在
^{pr2}$生成Pytest夹具
为了让您的生活更轻松,我们将以上所有内容都简化为pytest夹具生成器。在
importboto3_fixturesasb3faws=b3f.contrib.pytest.moto_fixture(services=["dynamodb","kinesis","sqs","s3","lambda"],scope="class",)sqs=b3f.contrib.pytest.service_fixture("sqs",scope="class",queues=fixtures.SQS)kinesis=b3f.contrib.pytest.service_fixture("kinesis",scope="class",streams=fixtures.KINESIS)dynamodb=b3f.contrib.pytest.service_fixture("dynamodb",scope="class",tables=fixtures.DYNAMODB)s3=b3f.contrib.pytest.service_fixture("s3",scope="class",buckets=fixtures.S3)lam=b3f.contrib.pytest.service_fixture("lambda",scope="class",lambdas=fixtures.LAMBDA)sns=b3f.contrib.pytest.service_fixture("sns",scope="class",topics=fixtures.TOPICS)# Example Usagedeftest_my_code(sqs):boto3.client("sqs").list_queues()
夹具
为了确保模拟云是服务fixture的依赖项,boto3 fixtures希望您创建一个名为aws
的fixture。如果您不利用这一点,您的本地云堆栈可能会在您的服务之前被拆除,导致在拆除服务时出现boto3异常。在
# Example: localstack via pytest-localstackimportpytest_localstackaws=pytest_localstack.patch_fixture(services=["sqs"],scope="class",)# Example: moto via boto3-fixturesimportboto3_fixturesasb3faws=b3f.contrib.pytest.moto_fixture(services=["sqs"],scope="class",)
配置服务
服务的配置可以是namesList[str]
的列表,也可以是包含bot3参数的configsList[dict]
的列表。在
Service | List of Names | List of Configs |
---|---|---|
s3 | yes | yes |
sqs | yes | yes |
kinesis | yes | yes |
dynamodb | yes | |
lambda | yes | |
sns | yes | yes |
例如,您的配置可能如下所示:
S3=["first-bucket","second-bucket"]SQS=["first-queue","second-queue"]KINESIS=["first-stream","second-stream"]DYNAMODB=[{"AttributeDefinitions":[{"AttributeName":"uri","AttributeType":"S"},{"AttributeName":"timestamp","AttributeType":"S"},],"TableName":"test-dbd-table","KeySchema":[{"AttributeName":"uri","KeyType":"HASH"},{"AttributeName":"timestamp","KeyType":"RANGE"},],}]LAMBDA=[{"zip_path":"dist/build.zip","FunctionName":"my_lambda","Runtime":"python3.6","Environment":{"foo":True},}]SNS=["my-topic-with-default-attrs",{"Name":"my-topic-with-additional-params","Tags":[{"Key":"key1","Value":"val1"}],"Attributes":{"DisplayName":"YourSystemIsOnFireTopic",},}]
这些配置不一定是静态的。您可以使用pytest fixture来构建或编译想要模拟的资源列表。在
同时使用moto
和localstack
您可以将aws
fixture指向moto或localstack,以显式地要求在服务fixture期间存在一个特定的堆栈。例如,如果使用两个堆栈:
# conftest.pystack_config={"services":["dynamodb","kinesis","sqs","s3","lambda"],"scope":"class","autouse":False,"region_name":"us-east-1",}localstack=pytest_localstack.patch_fixture(**stack_config)moto=b3f.contrib.pytest.moto_fixture(**stack_config)@pytest.fixture(scope="class")defaws(moto):pass# component/conftest.py@pytest.fixture(scope="class")defaws(localstack):pass
已知问题
- 如果首先运行pytestlocalstack测试,那么在同一个项目中同时使用pytestlocalstack和moto可能会中断。有人怀疑这是由于pytest localstack会话的清理问题引起的,但这是否仍在调查中。在
- 项目
标签: