Moto

Users can test AWS dependent code using the create_moto_fixture.

pytest_mock_resources.create_moto_fixture(scope='function')

Produce a Moto fixture.

Any number of fixture functions can be created. Under the hood they will all share the same moto server.

Note

Each test executes using a different (fake) AWS account through moto. If you create boto3 client/resource objects outside of the one handed to the test (for example, in the code under test), they should be sure to use the aws_access_key_id, aws_secret_access_key, aws_session_token, and endpoint_url given by the pmr_moto_credentials fixture.

Note

A moto dashboard should be available for debugging while the container is running. By default it would be available at http://localhost:5555/moto-api/# (but the exact URL may be different depending on your host/port config.

Parameters

scope (str) – The scope of the fixture can be specified by the user, defaults to “function”.

Consider the following example:

# src/some_module.py

def list_files(s3_client):
    return s3_client.list_objects_v2(Bucket="x", Key="y")

A user could test this as follows:

# tests/some_test.py

from pytest_mock_resources import create_moto_fixture
from some_module import list_files

moto = create_moto_fixture()

def test_list_files(moto):
    s3_client = moto.client("s3")
    files = list_files(s3_client)
    assert ...

The test is handed a proxy-object which should functionally act like a boto3.Session object. Namely you would generally want to call .client(…) or .resource(…) on it.

Note

Each test executes using a different (fake) AWS account through moto. If you create boto3 client/resource objects using boto3 directly, outside of the object handed to your test, you should make sure to pass all the credentials fields into the constructor such that it targets the correct AWS instance/account.

For example:

import boto3

def test_list_files(pmr_moto_credentials):
    kwargs = pmr_moto_credentials.as_kwargs()
    s3_client = boto3.client("s3", **kwargs)

Note

A moto dashboard should be available for debugging while the container is running. By default it would be available at http://localhost:5555/moto-api/# (but the exact URL may be different depending on your host/port config.