Testing - Serverless Architectures with AWS Lambda

Testing

Time spent developing thorough testing of your code is the best way to ensure quality within a serverless architecture. However, serverless architectures will enforce proper unit testing practices perhaps more than you’re used to. Many developers use unit test tools and frameworks to write tests that cause their code to also test its dependencies. This is a single test that combines a unit test and an integration test but that doesn’t perform either very well.

It’s important to scope all of your unit test cases down to a single code path within a single logical function, mocking all inputs from upstream and outputs from downstream. This allows you to isolate your test cases to only the code that you own. When writing unit tests, you can and should assume that your dependencies behave properly based on the contracts your code has with them as APIs, libraries, etc.

It’s similarly important for your integration tests to test the integration of your code to its dependencies in an environment that mimics the live environment. Testing whether a developer laptop or build server can integrate with a downstream dependency isn’t fully testing if your code will integrate successfully once in the live environment. This is especially true of the Lambda environment, where you code doesn’t have ownership of the events that are going to be delivered by event sources and you don’t have the ability to create the Lambda runtime environment outside of Lambda.