Introduction Python’s Moto Library- Easily Mock out AWS Services | by Eldad Uzman | Apr, 2022

Add extra validity to your exams

Photograph by Sunrise King on Unsplash

Unit exams are our first line of protection towards regressive code adjustments.
In case your Python code includes the utilization of AWS sources, you may discover this text helpful to your testing protection.

Resource optimism is a take a look at scent that happens when a take a look at case makes an optimistic assumption about an exterior useful resource.

Both the take a look at perform itself makes use of an exterior useful resource, or the elements of code that it exams makes use of an exterior useful resource — which isn’t assured to be accessible at take a look at execution time.

The end result of useful resource optimism may very well be flakiness in take a look at execution, in different phrases, the take a look at exhibits a cross or fail consequence when executed towards the identical code base.

One other drawback may very well be the necessity to handle these exterior sources within the take a look at setup, making it unrealistic and cumbersome.

AWS sources reminiscent of S3 buckets or sqs queues are exterior sources, and as such, they need to be thought to be suspects for optimism.

Due to this fact, when working our unit exams, we need to ensure that they don’t depend upon accessible AWS sources. The widespread follow to attain that’s mocking.

Moto is an open supply library that gives a straightforward abstraction to Python’s built-in unit take a look at mock library.

Its documentation is superior, and it helps a wealthy set of AWS functionalities, relieving the necessity to develop mocks by yourself.
This fashion you may concentrate on writing your unit take a look at to excessive protection.

Let’s take a look at a easy Python class:

Challenge class defines a state of a undertaking.

When initialized, it creates two sources: one is an s3 bucket; the opposite is the SQS queue, which additionally generates a consumer object to AWS SQS and s3 utilizing the boto3 library.

Along with that, it has a state, which is expressed as a UUID.

When save is known as, the state adjustments, and the enter message is saved into an s3 bucket.

When restore is known as, the beforehand saved message is known as from s3, after which a notification is distributed to the SQS queue.

After that, the message is returned to the outer context.

Let’s write a easy unit take a look at:

This take a look at is horrible because it depends on the exterior AWS sources, however because of moto, this may be simply improved with solely three strains of code (lazy programmers will respect that :D).

First, let’s set up moto from PyPI:

pip set up moto

Now, let’s use moto in our take a look at code:

First, we import the s3_mock andsqs_mock from moto.

These two are decorators, and so they can beautify both features or courses, making a mock out of all AWS useful resource dealing with calls within the adorned code block.

Now we are able to beautify the category TestMyAws and in all subsequent code blocks, AWS will likely be mocked.

On this code, we are able to additionally see that we don’t must handle secrets and techniques since all calls are mocked.

Let’s run the take a look at:

python -m unittest take a look at/


Ran 1 take a look at in 1.074s

In our init take a look at code, we wish as a lot isolation as potential.

Nonetheless, this doesn’t at all times apply to extra sturdy exams, integration exams, API exams, regression exams, or E2E.

Typically, we do want to make use of the precise useful resource or to make use of mock servers.

Moto permits you to write steady exams and to be extra concise in your take a look at code.

Thanks for studying.

More Posts