Python Test Samples Starter ProjectLocal Testing4 min

Working with events

Testing event driven architectures often requires working with synthetic events. You can define events as JSON documents. Synthetic events are test data that represent AWS events such as a requests from API Gateway or messages from SQS.

AWS Lambda always requires an event during invocation. This project includes a sample test event in the events folder. SAM provides the capability of generating additional synthetic events for a variety of AWS services. Read the documentation.

Use the following command to learn more about generating synthetic events:

# generate a synthetic event
 sam local generate-event

Working with local emulators

Local emulation of AWS services offers a simple way to build and test cloud native applications using local resources. Local emulation can speed up the build and deploy cycle, creating faster feedback loops for application developers.

Local emulation has several limitations. Cloud services evolve rapidly, so local emulators are unlikely to have feature parity with their counterpart services in the cloud. Local emulators may not provide an accurate representation of IAM permissions or service quotas. Local emulators do not exist for every AWS service.

SAM provides local emulation features for AWS Lambda and Amazon API Gateway. AWS provides Amazon DynamoDB Local and AWS Step Functions Local. Third-party vendors like LocalStack may provide emulation for additional AWS services.

This project demonstrates local emulation of Lambda and API Gateway with SAM.

Use the SAM Lambda emulator

The SAM CLI can emulate a Lambda function inside a Docker container deployed to your local desktop. To use this feature, invoke the function with the sam local invoke command passing a synthetic event. Print statements log to standard out. Read the documentation.

# invoke a Lambda function locally
 sam local invoke PythonTestDemo --event events/event.json

The sam local start-lambda command starts a local endpoint that emulates the AWS Lambda invoke endpoint. You can invoke it from your automated tests. Because this endpoint emulates the AWS Lambda invoke endpoint, you can write tests and then run them against the local Lambda emulator. You can also run the same tests against a deployed AWS SAM stack in your CI/CD pipeline. Read the documentation.

# start a local emulator for a Lambda function endpoint
 sam local start-lambda --region us-east-1
# run a unit test in a separate terminal
 python3 -m pytest -s tests/unit/local_emulator_test.py -v

Use the SAM API Gateway emulator

The SAM CLI can also emulate your application's API. Use the sam local start-api to run the API locally on port 3000.

# start a local emulator for an API Gateway endpoint
 sam local start-api
# make a request to the endpoint in a separate terminal
 curl http://localhost:3000/hello

The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The Events property on each function's definition includes the route and method for each path.

      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

Run a unit test using a mock framework

Lambda functions frequently call other AWS or 3rd party services. Mock frameworks are useful in simulating service responses. Mock frameworks can speed the development process by enabling rapid feedback iterations. Mocks can be useful for testing failure cases when testing these branches of logic is difficult to do in the cloud.

This project uses mocks to test the internal logic of a Lambda function. The project uses the moto dependency library to mock an external service call to Amazon S3. The moto library can simulate responses from a variety of AWS services. We defined tests with mocks in the tests/unit folder. Use pip to install test dependencies and pytest to run the unit test.

# Create and Activate a Python Virtual Environment
 pip3 install virtualenv
 python3 -m venv venv
 source ./venv/bin/activate

# install dependencies
 pip3 install -r tests/requirements.txt

# run unit tests with mocks
 python3 -m pytest -s tests/unit/mock_test.py -v

Created by:

Dan Fox
Dan FoxDan Fox is a Principal Specialist Solutions Architect at AWS. Dan works with customers to help them leverage serverless services to build scalable, fault-tolerant, high-performing, cost-effective applications.
Rohan Mehta
Rohan MehtaAssociate Cloud Application Architect at AWS.