# Moto — Mock AWS Services for Testing in Python > Moto is a Python library that mocks AWS services for unit and integration testing. It provides a decorator and context manager API that intercepts boto3 calls and returns realistic responses, letting you test AWS-dependent code without hitting real services. ## Install Save in your project root: # Moto — Mock AWS Services for Testing in Python ## Quick Use ```bash pip install moto[s3,dynamodb] ``` ```python import boto3 from moto import mock_aws @mock_aws def test_s3_upload(): s3 = boto3.client("s3", region_name="us-east-1") s3.create_bucket(Bucket="my-bucket") s3.put_object(Bucket="my-bucket", Key="test.txt", Body=b"hello") obj = s3.get_object(Bucket="my-bucket", Key="test.txt") assert obj["Body"].read() == b"hello" ``` ## Introduction Moto is a testing library that mocks the entire AWS API surface in Python. Instead of connecting to real AWS services, your boto3 calls are intercepted and handled by in-memory implementations that behave like the real thing. This makes tests fast, reliable, free from AWS costs, and safe to run in CI environments without AWS credentials. ## What Moto Does - Mocks 150+ AWS services including S3, DynamoDB, Lambda, SQS, SNS, EC2, IAM, and more - Intercepts boto3 client and resource calls transparently via decorators or context managers - Maintains in-memory state so that resources created in one call are visible to subsequent calls - Supports both standalone use in tests and a server mode for non-Python clients - Provides realistic error responses when operations violate AWS API constraints ## Architecture Overview Moto intercepts HTTP requests at the botocore transport layer using responses or urllib3 mocking. Each AWS service has a backend class that implements the service's API operations with in-memory data structures. When a boto3 call is made within a Moto context, the request is routed to the matching backend, which processes it and returns a response that matches the real AWS API format. State persists within the mock context and is discarded when the context exits. ## Self-Hosting & Configuration - Install with `pip install moto` for all services, or `pip install moto[s3,dynamodb]` for specific ones - Use the `@mock_aws` decorator on test functions or `with mock_aws():` as a context manager - Start a standalone mock server with `moto_server` for testing non-Python clients over HTTP - Configure default account ID and region via environment variables if needed - Works with pytest, unittest, and any other Python test framework ## Key Features - Coverage of 150+ AWS services with ongoing additions tracking the real AWS API - Stateful mocking where creating a resource in one call makes it findable in the next - Zero AWS credentials required for tests, making CI pipelines simpler and cheaper - Standalone server mode for testing non-Python applications against a mock AWS endpoint - Drop-in usage with no changes to production code; only test setup needs the mock decorator ## Comparison with Similar Tools - **LocalStack** — runs real service emulators in Docker; more realistic but heavier and slower to start - **botocore stubber** — built into botocore for manual response stubbing; lower-level with no automatic state management - **Fake AWS services (e.g., MinIO)** — full service implementations; useful for integration testing but require running processes - **pytest-mock / unittest.mock** — generic mocking; can mock boto3 but requires manual response construction - **AWS SAM Local** — emulates Lambda and API Gateway locally; narrower scope but runs actual Lambda code ## FAQ **Q: Does Moto work with aiobotocore and async code?** A: Yes. Moto supports aiobotocore for async AWS calls in the same decorator/context manager pattern. **Q: How accurate are the mocked responses?** A: Moto aims to match AWS API behavior closely. Most common operations are well-covered, though edge cases in newer or less-used services may differ. **Q: Can I use Moto in integration tests with Docker?** A: Yes. Run `moto_server` in a Docker container and point your application's AWS endpoint URL to it. **Q: Does it support CloudFormation?** A: Yes. Moto has a CloudFormation backend that can process templates and create mocked resources. ## Sources - https://github.com/getmoto/moto - https://docs.getmoto.org/ --- Source: https://tokrepo.com/en/workflows/asset-77cb8b2f Author: AI Open Source