Do you need to test some pieces of code interacting with AWS? In this article, I’m going to introduce the Moto library whose aim is to mock out AWS services. That means you won’t have to deploy anything in the AWS cloud to test your code.
In the first part, we’ll create a simple piece of code interacting with the Elastic Compute (EC2) service. We’ll examine the different ways of mocking provided by Moto. This will give you an overview of its possibilities.
Next, we’ll couple the Moto library and pytest framework to have clean and scalable unit tests.
First of all, we need to install the different Python libraries required using
$ python3 -m pip install boto3 moto pytest
Overview of the Moto Library
“Moto is a library that allows your tests to easily mock out AWS Services.” — moto on GitHub
Let’s create a
main.py containing a simple function that creates EC2 instances:
Moto is not limited to the EC2 service. It supports many other AWS services to mock out. Also, the library offers multiple usages that we’ll see in the following sections.
We will be going over two tests:
- Assert that there is the instance count as expected.
- Assert that the EC2 instance is running the correct Amazon Machine Image (AMI).
AWS services can be mocked out using a simple decorator:
You can also use Moto mocks as a context manager:
And finally, you can go to the raw approach:
Couple Moto With Pytest
Pytest is a framework to create small and scalable unit tests. It provides test fixtures that are functions running before the test functions. It is great to feed some data or set up a database connection, for example. Here, we’re going to define fixtures to configure S3 mocking and fake AWS credentials. By default, pytest loads the content of
test.py, we use a context manager to create S3 before running the tests present in a dedicated class. There is a test checking if the S3 bucket exists and another testing the placement of an object:
Launch the unit tests with the
$ pytest test.py
In this article, we’ve seen how to mock AWS responses using Moto. This allows for free and quick testing of your Python code. Coupled with pytest, it provides scalable and clean unit tests for AWS interactions.
Join the newsletter to receive the latest updates in your inbox.