使用boto3测试aws依赖性的工件
awstestutils的Python项目详细描述
Author: | Elvio Toccalino |
---|---|
Date: | 2019/05/20 |
Version: | 1.0.0 |
测试工件以使用boto3库。
重点是python 3和^{str1}$boto3。到目前为止,utils涵盖了sqs队列和sns主题。
平方秒
livetestqueue允许快速测试依赖于sqs队列的代码。
>>> with LiveTestQueue() as queue: >>> queue.send_message(MessageBody='some') >>> msgs = queue.receive_messages() >>> print(msgs[0].body)
上下文管理器负责创建和最终删除队列,并确保队列具有唯一的名称(前缀为“test”队列)。
sns
livetesttopicqueue允许测试主题上的代码。
>>> with LiveTestTopicQueue() as topic, queue: >>> topic.publish(Message='some') >>> msgs = queue.receive_messages() >>> print(msgs[0].body)
上下文管理器创建(并最终删除)一对共同工作的对象,一个主题和一个队列。发布到主题的消息可以在队列中变回红色。主题有适当的策略发布到队列,队列订阅主题作为其端点。
发电机
livetestdynamodbtable允许使用live test dynamodb表进行测试:
>>> key_schema, attribute_definitions, provisioned_throughput = LiveTestDynamoDBTable.create_key_schema( >>> partition_key_name='my_partition_key', sorting_key_name='my_sorting_key', >>> partition_key_type='S', sorting_key_type='N', read_capacity_units=1, write_capacity_units=1) >>> with LiveTestDynamoDBTable(key_schema_definition=key_schema, >>> attribute_definitions=attribute_definitions, >>> provisioned_throughput=provisioned_throughput) as table:
注意helper函数创建键模式。退出上下文管理器,删除测试表。
其他
- 减少日志输出量()
- 快速减少botocore的日志输出量,以简化其他组件的调试。
- 清理()
- 删除可能留下的测试主题和队列。此函数也可以作为脚本调用,使用python -m awstestutils.cleanup。
测试
该包包括一组集成测试。这些测试针对aws后端的活动对象,因此网络必须启动并且boto3必须正确配置(as described here)。
示例
示例测试可以在examples.py中找到。
测试使用主题发送数据的对象:
with LiveTestQueue() as queue: o = ObjectUnderTest(sqs_queue=queue) o.do_something() o.send_results_to_backend() msgs = queue.receive_messages() self.assertEqual(len(msgs), 1) expected = json.dumps(expected_output) self.assertEqual(msgs[0].body, expected)
测试发布到主题的对象,检查发布的消息:
with LiveTestTopicQueue() as (topic, queue): o = ObjectUnderTest(topic) o.do_something() o.send_results_to_aws() msgs = queue.receive_messages() expected = json.dumps(expected_output) self.assertEqual(msgs[0].body, expected)
合作者
- facundo martinez(@fnmartinez)为dynamodb及其相关测试添加了支持,这是一项了不起的工作。
- Ezequiel pochiero(@epochiero)修复了最初管理地区名称的错误。
谢谢你们俩!