Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 27, 2026·3 min de lecture

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.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Moto AWS Mocking
Commande d'installation directe
npx -y tokrepo@latest install 77cb8b2f-5a0a-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires