Moto is a Python library that allows developers to easily mock AWS services for testing purposes. It intercepts Boto3 calls and routes them to an in-memory mock, simulating AWS API behavior locally without actual AWS interaction. The current version is 5.1.22, and it typically sees new releases every 1-2 weeks, ensuring continuous updates for AWS service support.
pip install moto[all]Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to mock an S3 service using the `@mock_aws` decorator. It creates a Boto3 S3 client within the mocked context, creates a bucket, and then verifies its existence, all without interacting with actual AWS infrastructure. Dummy AWS credentials are set to prevent botocore from attempting real AWS calls.
Replace specific decorators like `@mock_s3` with `@mock_aws`. If you need to specify specific services for the mock, pass them as arguments to `@mock_aws` (though often not necessary for basic usage, it mocks all by default). For example: `@mock_aws(config={'s3': {'use_docker': False}})`.Create `boto3` clients/resources inside the `@mock_aws` decorated function, within the `with mock_aws():` block, or within a `setUp` method of a `unittest.TestCase` where the class is decorated. For Pytest, ensure fixtures that create clients are decorated or called within the mock's scope. Using local imports for modules that create `boto3` clients can also help.
Include `region_name='your-desired-region'` in all `boto3.client()` and `boto3.resource()` calls within your mocked tests.
If your tests relied on shared state between methods in a class, refactor them to ensure each test method sets up its required state independently, or use alternative mechanisms (e.g., pytest fixtures with appropriate scope) to manage state if shared setup is truly necessary.
Decorate your pytest fixtures with `@mock_aws` if they provision AWS clients, or use the `mock_aws` context manager within the fixture's setup phase. Example: `@pytest.fixture @mock_aws def s3_client(): return boto3.client('s3', region_name='us-east-1')`.Before running tests, set environment variables like `AWS_ACCESS_KEY_ID=testing`, `AWS_SECRET_ACCESS_KEY=testing`, `AWS_SECURITY_TOKEN=testing`, `AWS_SESSION_TOKEN=testing`, and `AWS_DEFAULT_REGION=us-east-1`. These can be exported in your test runner or set programmatically.
Install the moto library using pip. If you need specific AWS service mocks, install them with extras (e.g., `[all]` or `[s3,dynamodb]`). ```bash pip install moto[all] # Or for specific services: pip install moto[s3,dynamodb] ```
Replace individual service decorators with the unified `@mock_aws` decorator.
```python
from moto import mock_aws
@mock_aws
def test_my_aws_function():
# Your test code here
pass
```Ensure that the `moto` decorator (`@mock_aws`) or context manager (`with mock_aws():`) is applied *before* any `boto3` clients or resources are created in your test. Also, it's good practice to set dummy AWS credentials for tests.
```python
import os
import boto3
from moto import mock_aws
# Set dummy credentials (optional, but good practice)
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
os.environ['AWS_SECURITY_TOKEN'] = 'testing'
os.environ['AWS_SESSION_TOKEN'] = 'testing'
os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'
@mock_aws
def test_s3_operation():
s3_client = boto3.client('s3', region_name='us-east-1')
# Your S3 operations
s3_client.create_bucket(Bucket='my-test-bucket')
```Create the necessary AWS resource (e.g., S3 bucket, DynamoDB table) within your `moto` decorated test function or fixture before making calls to it.
```python
import boto3
from moto import mock_aws
@mock_aws
def test_list_s3_buckets():
s3_client = boto3.client('s3', region_name='us-east-1')
s3_client.create_bucket(Bucket='my-test-bucket') # Create the bucket in the mock environment
response = s3_client.list_buckets()
assert 'my-test-bucket' in [b['Name'] for b in response['Buckets']]
```