AWS SDK Testing Best Practices
A testing pyramid for AWS SDK-calling code, in both Python and TypeScript.
Busque em todas as páginas da documentação
A testing pyramid for AWS SDK-calling code, in both Python and TypeScript.
Walk the list once now, then use it as a review checklist whenever you add tests around new SDK-calling code.
@mock_aws (moto) in Python and mockClient(...) (aws-sdk-client-mock) in TypeScript; both run in milliseconds with no network access.@mock_aws is the unified decorator in moto v5; the older per-service decorators (@mock_s3, @mock_dynamodb) are deprecated.ClientError/typed SDK exception and assert your code handles it - this is usually where real bugs hide.Isolate at the client boundary and keep unit tests fast.
# --- Python (boto3) ---
import boto3
from moto import mock_aws
@mock_aws
def test_create_bucket():
s3 = boto3.client("s3", region_name="us-east-1")
s3.create_bucket(Bucket="my-bucket")
assert "my-bucket" in [b["Name"] for b in s3.list_buckets()["Buckets"]]// --- TypeScript (AWS SDK v3) ---
import { S3Client, CreateBucketCommand } from "@aws-sdk/client-s3";
import { mockClient } from "aws-sdk-client-mock";
const s3Mock = mockClient(S3Client);
test("creates a bucket", async () => {
s3Mock.on(CreateBucketCommand).resolves({});
await new S3Client({}).send(new CreateBucketCommand({ Bucket: "my-bucket" }));
expect(s3Mock).toHaveReceivedCommandWith(CreateBucketCommand, { Bucket: "my-bucket" });
});aws-sdk-client-mock instances persist stubs at module scope - call .reset() in beforeEach; moto isolates state per @mock_aws-decorated function automatically.toHaveReceivedCommandWith (aws-sdk-client-mock) or read seeded/mutated state back (moto) to catch bugs where the wrong parameters are sent.err.response["Error"]["Code"] (boto3) or instanceof SpecificException (SDK v3); message text can change without notice.forcePathStyle: true (SDK v3) or the boto3 equivalent; virtual-hosted-style URLs need real DNS LocalStack doesn't provide.finally/fixture-teardown/afterAll, even on failure, so nothing leaks into a growing, invisible cost.Default to fast, in-process mocks for the vast majority of tests, and only escalate to LocalStack or real AWS when the specific behavior genuinely requires it.
moto v5 unified every service-specific decorator into one @mock_aws decorator that covers all services at once; the old per-service decorators still work but are deprecated.
A handful, focused specifically on things mocks and LocalStack can't verify - real IAM permission edges and genuine service quirks - not a re-run of your whole suite.
Forgetting to call .reset() between tests. Because the mock is module-scoped, a stub or call history from one test can silently leak into and affect a later one.
When you need to verify cross-service behavior, like an S3 event actually triggering a downstream consumer, that an in-process mock has no way to simulate.
Generally no. Run them on a schedule or as a pre-release gate so real AWS latency and rare transient errors don't slow down or destabilize the everyday CI feedback loop.
The request your code actually sent. Matchers like toHaveReceivedCommandWith (aws-sdk-client-mock) or reading seeded state back (moto) catch bugs where parameters are wrong even though the mocked call "succeeds."
For small projects with simple, well-documented AWS usage, maybe. But any code relying on specific IAM boundaries or quota behavior benefits from at least a small, scheduled contract suite.
Stack versions: This page was written for boto3 1.43.x (Python 3.10+) and the AWS SDK for JavaScript v3 (Node.js 18+).
Revisado por Chris St. John·Última atualização: 23 de jul. de 2026