We don’t have an SQS construct in our stack but reviewing the example test provides some level of insight on how to test a construct. Let’s delete the auto generated example test and create our own test.

Setting Up The Testing Function

First, let’s create a reusable function to get the CloudFormation template from the stack:

Testing DynamoDB Table Properties

We’ll start by adding a test to check that the DynamoDB table in our stack has the correct properties. Here’s how to do it:

Testing Lambda Functions

Next, let’s ensure that our stack creates the correct number of Lambda functions and verifies their runtime version:

Running Tests

To run tests you can execute pytest in the terminal:

pytest

Below is the output you should receive:

================================================================================= test session starts =================================================================================platform darwin -- Python 3.12.4, pytest-8.1.1, pluggy-1.4.0rootdir: /Users/adrian/Developer/Projects/rss-lambda-ddb-socialshareplugins: typeguard-2.13.3collected 2 items                                                                                                                                                                     tests/unit/test_rss_lambda_ddb_socialshare_stack.py ..                                                                                                                          [100%]================================================================================= 2 passed in 21.86s ==================================================================================

Conclusion

In Part 3 of our series, we’ve learned how to test our CDK constructs. In the final installment, we will explore how to test Lambda functions locally.

Resources

","image":"http://www.luping.net/uploads/20240815/172369836366bd8cbb7a11d.jpg","datePublished":"2024-08-15T13:06:03+08:00","dateModified":"2024-08-15T13:06:03+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Exploring AWS Serverless Deployments with CDK vFrom RSS to X Posts - Part f the Odyssey

Exploring AWS Serverless Deployments with CDK vFrom RSS to X Posts - Part f the Odyssey

Published on 2024-08-15
Browse:754

Exploring AWS Serverless Deployments with CDK vFrom RSS to X Posts - Part f the Odyssey

Welcome to part 3 of “Exploring AWS Serverless Deployments with CDK v2”. Firstly I’d like to thank you for your patience as there’s been a bit of a gap since part 2. I was deep into studying and working on serverless projects at work which kept me away, but i’m excited to get back on track and continue our exploration.

In previous posts, we’ve defined our constructs and deployed them to AWS. Today, we’ll focus on an essential practice: testing. Proper testing ensures that our deployments work as expected and can save us from potential issues.

Getting Started With Testing

To get started, you’ll need to add pytest to your project’s dependencies (the main requirements.txt file for our stack).

pip install -r requirements.txt

Within our project, navigate towards the test directory, then unit and open the test_rss_lambda_ddb_socialshare_stack.py file. This auto generated test file includes an example test.

We don’t have an SQS construct in our stack but reviewing the example test provides some level of insight on how to test a construct. Let’s delete the auto generated example test and create our own test.

Setting Up The Testing Function

First, let’s create a reusable function to get the CloudFormation template from the stack:

Testing DynamoDB Table Properties

We’ll start by adding a test to check that the DynamoDB table in our stack has the correct properties. Here’s how to do it:

Testing Lambda Functions

Next, let’s ensure that our stack creates the correct number of Lambda functions and verifies their runtime version:

Running Tests

To run tests you can execute pytest in the terminal:

pytest

Below is the output you should receive:

================================================================================= test session starts =================================================================================
platform darwin -- Python 3.12.4, pytest-8.1.1, pluggy-1.4.0
rootdir: /Users/adrian/Developer/Projects/rss-lambda-ddb-socialshare
plugins: typeguard-2.13.3
collected 2 items                                                                                                                                                                     

tests/unit/test_rss_lambda_ddb_socialshare_stack.py ..                                                                                                                          [100%]

================================================================================= 2 passed in 21.86s ==================================================================================

Conclusion

In Part 3 of our series, we’ve learned how to test our CDK constructs. In the final installment, we will explore how to test Lambda functions locally.

Resources

  • Pytest
  • Testing constructs
Release Statement This article is reproduced at: https://dev.to/aws-builders/exploring-aws-serverless-deployments-with-cdk-v2-from-rss-to-x-posts-part-3-of-the-odyssey- 56c3?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3