-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(aws-lambda-python): standard pattern for Lambda unit tests #17048
Comments
Probably, yeah. Though, I'm not sure if tests on Lambda functions should occur as a part of the tests for setting up the infra, i.e. at the CDK layer. There's 2 points at which tests might occur:
So, I'm not sure this should be a part of the build process for the infrastructure. It definitely should be a part of the CI / CD process that deploys the infrastructure, though, and any of the above could be executed as a part of those steps. |
So what I'm talking about is at the function code test, the unit test level, your point number 1. A complement to the unit tests that assert the CDK infrastructure you have defined is what you think it is. For other languages, running unit tests is implicitly part of building code. For example, I have an application that includes Java lambda functions that are being built using Gradle, and unit tests are defined as part of that Gradle build and executed during it according to standard Gradle practices. |
Yeah, that makes sense. I run them using |
As mentioned above, unit testing Python lambda handler code is like unit testing any python code. |
There's a lot more to unit testing Python lambda handler code than choosing
Once you look at this problem in more detail, it's not at all simple. I've put together a framework for unit testing my CDK python Lambdas, but it's not robust enough I'd share it as any kind of best practice. This is why I am requesting a standard pattern. At the moment, anyone who wants to unit test their Python Lambdas with the CDK has to solve these non-trivial problems. I suspect there's people who are not unit testing their CDK Python Lambdas who would be if there was standard guidance for them to draw upon. It'd be nice if one person solved this problem well, rather than every CDK Python Lambda development team solving this problem poorly, or not at all. |
Thanks for the detailed explanation @SamStephens. All of these are valid considerations and solving this in a single effort would be very valuable to Python users. However, none of these seem specific to the AWS CDK. This will apply to any Python project that is modeling Lambda handlers. So this might be the wrong forum for this. If you feel passionately about solving this problem, there are a couple of options. |
I also needed a way to test python container lambdas. cdk only builds container images during deploy, which is too late for testing. A way was needed to build the container images as defined in cdk and test them before deploying. For this I created a simple module cdk-image-testing, that uses cdk-assets to build images as defined in the cloud assembly. Example: Dockerfile
./test/container-image.test.ts
|
Description
It'd be great to have official guidance/patterns as to how to do unit tests of the code a Python lambda contains. I've failed to find any official guidance for either aws-lambda-python or aws-lambda in general. It's hard to find bloggers who have solved this challenge also.
Use Case
So I can solve this problem in a standard way other CDK developers will know how to work with. So that the CDK has parity with other frameworks that make unit testing Lambda code easy.
Proposed Solution
An example application in an examples repository with a link from the documentation.
Other information
No response
Acknowledge
The text was updated successfully, but these errors were encountered: