简体   繁体   中英

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. So i'm planning to automate testing using python. Can any one help out how I can automate this lambda function to test the following using unittest package.

  1. Verify the dynamodb table existency
  2. Validate whether the bucket exists or not in s3 for event trigger.
  3. Verify the file count in s3 bucket and record count in Dynamodb table.

This can be done using moto and unittest . 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. Instead, moto will build up state in memory.

For example, you could

  1. Activate the mock for DynamoDB
  2. create a DynamoDB table
  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 .

I wrote up a tutorial on how to do this (it uses pytest instead of unittest but that should be a minor difference). Check it out: joshuaballoch.github.io/testing-lambda-functions/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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