本地AWS云堆栈的Python测试工具

boto3-fixtures的Python项目详细描述


boto3固定装置

PyPI versionTravisCI build statusCode Coverage

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]的列表。在

ServiceList of NamesList of Configs
s3yesyes
sqsyesyes
kinesisyesyes
dynamodbyes
lambdayes
snsyesyes

例如,您的配置可能如下所示:

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来构建或编译想要模拟的资源列表。在

同时使用motolocalstack

您可以将awsfixture指向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会话的清理问题引起的,但这是否仍在调查中。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将cassandra中的行数据转换为与列相关的嵌套json   java如何使用jcr XPath在jcr:content/@jcr:data中搜索?   java在使用openCV进行安卓开发时如何利用手机的广角镜头   java解析扩展了接口,结束了一个潜在的无限循环   位置服务的@Override方法中存在java Android应用程序错误   java本地线程的用途和需求是什么   具有左右子访问的java节点树遍历   java验证JsonWebToken签名   JUL日志处理程序中的java日志记录   嵌入式Java读取给定时间段的串行数据。   java有没有办法从多个URL获取多个图像?   java线程通过等待intent阻止自己发送intent   java Spring MVC解析多部分内容请求   java JPA/Hibernate静态元模型属性未填充NullPointerException   java格式错误的字符(需要引号,得到I)~正在处理   java为什么PrintWriter对象抛出FileNotFoundException?   java Neo4j未正确保存标签   java IE不加载图像