Unit testing is a fundamental aspect of software development, ensuring that individual components or units of code function as expected. For developers working with AWS Lambda functions and REST APIs, unit testing becomes even more critical due to the nature of serverless architecture. AWS Lambda allows you to run code without provisioning or managing servers, which can simplify deployment but also introduces unique challenges in testing. In this comprehensive guide, we’ll explore the essentials of unit testing AWS Lambda functions that serve as REST API endpoints, providing detailed explanations and practical examples.
This article is designed for computer science students and software development beginners, offering a clear understanding of unit testing concepts and their application in a real-world scenario.
What is AWS Lambda?
AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). It allows you to run code without provisioning or managing servers. You only pay for the compute time you consume, making it a cost-effective solution for building scalable applications.
Key Features of AWS Lambda
- Event-Driven Execution: AWS Lambda functions are triggered by various events, such as HTTP requests, database updates, or file uploads.
- Auto Scaling: AWS Lambda automatically scales your application by running code in response to each event, handling any number of requests simultaneously.
- Cost Efficiency: You pay only for the compute time you use, with no charges when your code is not running.
- Integrated with Other AWS Services: AWS Lambda seamlessly integrates with other AWS services like S3, DynamoDB, API Gateway, and more.
What is Unit Testing?
Unit testing is a software testing method where individual units or components of a software application are tested in isolation from the rest of the application. The primary goal of unit testing is to validate that each unit of code performs as expected.
Why Unit Testing?
- Detects Bugs Early: Unit tests help identify issues at an early stage, making them easier and less costly to fix.
- Improves Code Quality: By ensuring that each part of the code works correctly, unit testing enhances overall code quality.
- Facilitates Refactoring: With unit tests in place, developers can confidently refactor code without fear of introducing new bugs.
- Documentation: Unit tests serve as documentation for the code, illustrating how different components are expected to behave.
Setting Up the Environment
Before we dive into unit testing AWS Lambda functions, we need to set up our development environment. This includes setting up AWS CLI, AWS SDK for Python (Boto3), and creating a Lambda function.
Prerequisites
- AWS Account: You need an AWS account to create and manage Lambda functions.
- AWS CLI: The AWS Command Line Interface (CLI) is a tool to manage your AWS services. You can install it from AWS CLI.
- Python: We will use Python for this tutorial. Ensure you have Python installed on your machine. You can download it from Python.org.
Installing AWS CLI and Boto3
To install the AWS CLI, run the following command:
pip install awscli
Next, install Boto3, the AWS SDK for Python:
pip install boto3
Configuring AWS CLI
Configure the AWS CLI with your AWS credentials:
aws configure
You’ll be prompted to enter your AWS Access Key ID, Secret Access Key, region, and output format.
Creating a Sample AWS Lambda Function
For this tutorial, we’ll create a simple AWS Lambda function that serves as a REST API endpoint. The function will perform basic CRUD (Create, Read, Update, Delete) operations on a list of items.
Defining the Lambda Function
Create a new directory for your Lambda function and navigate to it:
mkdir lambda_function
cd lambda_function
Create a file named lambda_function.py
:
import json
items = []
def lambda_handler(event, context):
http_method = event.get("httpMethod")
if http_method == "GET":
return get_items()
elif http_method == "POST":
return add_item(event)
elif http_method == "PUT":
return update_item(event)
elif http_method == "DELETE":
return delete_item(event)
else:
return {
"statusCode": 405,
"body": json.dumps({"message": "Method Not Allowed"})
}
def get_items():
return {
"statusCode": 200,
"body": json.dumps(items)
}
def add_item(event):
try:
item = json.loads(event.get("body"))
items.append(item)
return {
"statusCode": 201,
"body": json.dumps({"message": "Item added"})
}
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": json.dumps({"message": "Invalid input"})
}
def update_item(event):
try:
item = json.loads(event.get("body"))
for i in range(len(items)):
if items[i]["id"] == item["id"]:
items[i] = item
return {
"statusCode": 200,
"body": json.dumps({"message": "Item updated"})
}
return {
"statusCode": 404,
"body": json.dumps({"message": "Item not found"})
}
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": json.dumps({"message": "Invalid input"})
}
def delete_item(event):
try:
item = json.loads(event.get("body"))
for i in range(len(items)):
if items[i]["id"] == item["id"]:
del items[i]
return {
"statusCode": 200,
"body": json.dumps({"message": "Item deleted"})
}
return {
"statusCode": 404,
"body": json.dumps({"message": "Item not found"})
}
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": json.dumps({"message": "Invalid input"})
}
This Lambda function handles four HTTP methods: GET, POST, PUT, and DELETE. It performs CRUD operations on a global list of items.
Packaging and Deploying the Lambda Function
To deploy the Lambda function, we’ll use the AWS CLI. First, zip the Lambda function code:
zip lambda_function.zip lambda_function.py
Next, create an IAM role for the Lambda function:
aws iam create-role --role-name lambda-execution-role --assume-role-policy-document file://trust-policy.json
The trust-policy.json
file should contain the following policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
Attach the AWS Lambda basic execution role:
aws iam attach-role-policy --role-name lambda-execution-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Deploy the Lambda function:
aws lambda create-function --function-name lambda-function --zip-file fileb://lambda_function.zip --handler lambda_function.lambda_handler --runtime python3.8 --role arn:aws:iam::<your_account_id>:role/lambda-execution-role
Replace <your_account_id>
with your AWS account ID.
Unit Testing AWS Lambda Functions
Now that we have our Lambda function set up, let’s move on to unit testing. Unit tests should focus on testing the Lambda function’s logic in isolation, without involving external services or dependencies.
Setting Up the Testing Environment
For unit testing, we’ll use the unittest
module, which is part of the Python standard library. Create a new file named test_lambda_function.py
in the same directory as your Lambda function.
touch test_lambda_function.py
Writing Unit Tests
Let’s write unit tests for the CRUD operations in our Lambda function.
import unittest
from lambda_function import lambda_handler, items
class TestLambdaFunction(unittest.TestCase):
def setUp(self):
# Clear the items list before each test
items.clear()
def test_get_items(self):
# Test GET request with no items
event = {"httpMethod": "GET"}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 200)
self.assertEqual(response["body"], "[]")
def test_add_item(self):
# Test POST request to add an item
event = {
"httpMethod": "POST",
"body": '{"id": 1, "name": "Test Item"}'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 201)
self.assertIn("Test Item", response["body"])
def test_update_item(self):
# Test PUT request to update an item
items.append({"id": 1, "name": "Old Item"})
event = {
"httpMethod": "PUT",
"body": '{"id": 1, "name": "Updated Item"}'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 200)
self.assertIn("Updated Item", response["body"])
def test_delete_item(self):
# Test DELETE request to delete an item
items.append({"id": 1, "name": "Test Item"})
event = {
"httpMethod": "DELETE",
"body":
'{"id": 1, "name": "Test Item"}'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 200)
self.assertIn("Item deleted", response["body"])
def test_invalid_method(self):
# Test with an invalid HTTP method
event = {"httpMethod": "PATCH"}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 405)
self.assertIn("Method Not Allowed", response["body"])
def test_invalid_input(self):
# Test with invalid input for POST request
event = {
"httpMethod": "POST",
"body": 'Invalid JSON'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 400)
self.assertIn("Invalid input", response["body"])
if __name__ == "__main__":
unittest.main()
Explanation of Tests
test_get_items
: Tests the GET request when there are no items. It expects a 200 status code and an empty list in the response body.test_add_item
: Tests the POST request to add a new item. It checks if the item is successfully added and the response contains the item name.test_update_item
: Tests the PUT request to update an existing item. It verifies that the item is updated correctly.test_delete_item
: Tests the DELETE request to remove an item. It ensures the item is deleted successfully.test_invalid_method
: Tests an invalid HTTP method (PATCH) and expects a 405 status code.test_invalid_input
: Tests invalid input for the POST request and expects a 400 status code.
Running the Tests
To run the tests, use the following command:
python test_lambda_function.py
The output will show the results of each test, indicating whether they passed or failed.
Real-Time Use Case: To-Do List API
Let’s extend our example to a real-time use case: a To-Do List API. In this scenario, the AWS Lambda function will manage a list of tasks, allowing users to add, update, delete, and retrieve tasks.
Extending the Lambda Function
Update the lambda_function.py
to handle task descriptions and completion status:
import json
tasks = []
def lambda_handler(event, context):
http_method = event.get("httpMethod")
if http_method == "GET":
return get_tasks()
elif http_method == "POST":
return add_task(event)
elif http_method == "PUT":
return update_task(event)
elif http_method == "DELETE":
return delete_task(event)
else:
return {
"statusCode": 405,
"body": json.dumps({"message": "Method Not Allowed"})
}
def get_tasks():
return {
"statusCode": 200,
"body": json.dumps(tasks)
}
def add_task(event):
try:
task = json.loads(event.get("body"))
tasks.append(task)
return {
"statusCode": 201,
"body": json.dumps({"message": "Task added"})
}
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": json.dumps({"message": "Invalid input"})
}
def update_task(event):
try:
task = json.loads(event.get("body"))
for i in range(len(tasks)):
if tasks[i]["id"] == task["id"]:
tasks[i] = task
return {
"statusCode": 200,
"body": json.dumps({"message": "Task updated"})
}
return {
"statusCode": 404,
"body": json.dumps({"message": "Task not found"})
}
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": json.dumps({"message": "Invalid input"})
}
def delete_task(event):
try:
task = json.loads(event.get("body"))
for i in range(len(tasks)):
if tasks[i]["id"] == task["id"]:
del tasks[i]
return {
"statusCode": 200,
"body": json.dumps({"message": "Task deleted"})
}
return {
"statusCode": 404,
"body": json.dumps({"message": "Task not found"})
}
except json.JSONDecodeError:
return {
"statusCode": 400,
"body": json.dumps({"message": "Invalid input"})
}
Updating the Unit Tests
Update the test_lambda_function.py
to reflect the changes:
import unittest
from lambda_function import lambda_handler, tasks
class TestLambdaFunction(unittest.TestCase):
def setUp(self):
# Clear the tasks list before each test
tasks.clear()
def test_get_tasks(self):
event = {"httpMethod": "GET"}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 200)
self.assertEqual(response["body"], "[]")
def test_add_task(self):
event = {
"httpMethod": "POST",
"body": '{"id": 1, "description": "Learn AWS Lambda", "completed": false}'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 201)
self.assertIn("Learn AWS Lambda", response["body"])
def test_update_task(self):
tasks.append({"id": 1, "description": "Old Task", "completed": false})
event = {
"httpMethod": "PUT",
"body": '{"id": 1, "description": "Updated Task", "completed": true}'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 200)
self.assertIn("Updated Task", response["body"])
def test_delete_task(self):
tasks.append({"id": 1, "description": "Test Task", "completed": false})
event = {
"httpMethod": "DELETE",
"body": '{"id": 1}'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 200)
self.assertIn("Task deleted", response["body"])
def test_invalid_method(self):
event = {"httpMethod": "PATCH"}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 405)
self.assertIn("Method Not Allowed", response["body"])
def test_invalid_input(self):
event = {
"httpMethod": "POST",
"body": 'Invalid JSON'
}
response = lambda_handler(event, None)
self.assertEqual(response["statusCode"], 400)
self.assertIn("Invalid input", response["body"])
if __name__ == "__main__":
unittest.main()
Running the Updated Tests
Run the updated tests with:
python test_lambda_function.py
The output will display the results of the tests, indicating whether they passed or failed.
Conclusion
Unit testing is a crucial practice in software development, ensuring that individual components of an application function correctly. This article provided a comprehensive guide to unit testing AWS Lambda functions, focusing on a real-time use case of a To-Do List API. We covered setting up the development environment, creating a Lambda function, and writing detailed unit tests.
By implementing unit tests, developers can ensure their code’s reliability and maintainability, making it easier to identify and fix bugs early in the development process. As you progress in your software development journey, mastering unit testing will be an invaluable skill, enabling you to build robust and high-quality applications.
Whether you’re a computer science student or a software development beginner, we hope this article has provided you with a solid foundation in unit testing AWS Lambda functions. Keep practicing and exploring different testing strategies to enhance your skills further. Happy coding!