How To Mock AWS Services in Python Unit Tests

How To Mock AWS Services in Python Unit Tests

Simulate and test your code interactions with AWS

Guillaume Vincent
Guillaume Vincent

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.

Prerequisites

First of all, we need to install the different Python libraries required using pip:

$ 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).

Decorator

AWS services can be mocked out using a simple decorator:

Context manager

You can also use Moto mocks as a context manager:

Raw use

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 conftest.py:

In 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 command:

$ pytest test.py

Conclusion

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.

Resources

pytest: helps you write better programs — pytest documentation
boto/boto3
AWS SDK for Python. Contribute to boto/boto3 development by creating an account on GitHub.
spulec/moto
A library that allows you to easily mock out tests based on AWS infrastructure. - spulec/moto
Programming

Guillaume Vincent

DevOps Engineer & AWS Certified Solution Architect. Cloud enthusiast and automation addict