简体   繁体   English

在python中自动测试aws lambda函数

[英]Automation testing for aws lambda functions in python

I have a aws lambda function which will write s3 file metadata information in dynamodb for every object created in s3 bucket, for this I have event trigger on s3 bucket. 我有一个aws lambda函数,它将在dynamodb中为s3存储桶中创建的每个对象写入s3文件元数据信息,为此我在s3存储桶上有事件触发器。 So i'm planning to automate testing using python. 所以我打算使用python自动化测试。 Can any one help out how I can automate this lambda function to test the following using unittest package. 任何人都可以帮助我如何使用unittest包自动化这个lambda函数来测试以下内容。

  1. Verify the dynamodb table existency 验证dynamodb表的存在性
  2. Validate whether the bucket exists or not in s3 for event trigger. 在s3中验证存储桶是否存在以进行事件触发。
  3. Verify the file count in s3 bucket and record count in Dynamodb table. 验证s3存储桶中的文件计数和Dynamodb表中的记录计数。

This can be done using moto and unittest . 这可以使用motounittest来完成。 What moto will do is add in a stateful mock for AWS - your code can continue calling boto like normal, but calls won't actually be made to AWS. moto将要做的是为AWS添加有状态模拟 - 您的代码可以继续像正常一样调用boto ,但实际上不会对AWS进行调用。 Instead, moto will build up state in memory. 相反, moto将在记忆中建立状态。

For example, you could 例如,你可以

  1. Activate the mock for DynamoDB 激活DynamoDB的模拟
  2. create a DynamoDB table 创建一个DynamoDB表
  3. Add items to the table 将项目添加到表中
  4. Retrieve items from the table and see they exist 从表中检索项目并查看它们是否存在

If you're building functionality for both DynamoDB and S3, you'd leverage both the mock_s3 and mock_dynamodb2 methods from moto . 如果您正在为DynamoDB和S3构建功能,那么您将利用motomock_s3mock_dynamodb2方法。

I wrote up a tutorial on how to do this (it uses pytest instead of unittest but that should be a minor difference). 我写了一个关于如何做到这一点的教程(它使用pytest而不是unittest但这应该是一个小的区别)。 Check it out: joshuaballoch.github.io/testing-lambda-functions/ 看看: joshuaballoch.github.io/testing-lambda-functions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM