FAQ
I have a Lambda function that performs calculations and returns a result without calling any other services. Do I really need to test this in the cloud?
Yes. Lambda functions have configuration parameters that could change the outcome of the
test. All Lambda function code has a dependency on timeout and memory
settings, which could cause the function to fail if they aren’t set properly. Lambda
policies also enable standard output logging to Amazon CloudWatch
How can testing in the cloud help with unit testing? If it’s in the cloud and connects to other resources, isn’t that an integration test?
We define unit tests as tests that operate on architectural components in isolation. This definition doesn’t necessarily preclude the use of service calls or other network communications.
Many serverless applications do have architectural components that can be tested in isolation, even in the cloud. A basic example is a Lambda function that takes some input, interprets it, and sends a message to an SQS queue. A unit test of such a function would likely test whether input values result in certain values being present in the queued message. Consider a test that is written by using the Arrange, Act, Assert pattern:
-
Arrange: Allocate resources (a queue to receive messages, and the function under test).
-
Act: Call the function under test.
-
Assert: Retrieve the message sent by the function, and validate the output.
A mock testing approach would involve mocking the queue with an in-process mock object, and creating an in-process instance of the class or module that contains the Lambda function code. During the Assert phase, the queued message would be retrieved from the mocked object.
In a cloud-based approach, the test would create an SQS queue for the purposes of the test, and would deploy the Lambda function with environment variables that are configured to use the isolated SQS queue as the output destination. After running the Lambda function, the test would retrieve the message from the SQS queue.
The cloud-based test would run the same code, assert the same behavior, and validate the application’s functional correctness. However, it would have the added advantage of being able to validate the settings of the Lambda function: the IAM role, IAM policies, and the function’s timeout and memory settings.