Interview Prep/Python for DevOps/Testing, services & metrics

What is botocore's `Stubber`, and when do you prefer it to moto?

AdvancedConceptTesting, services & metrics
Answer

Stubber queues exact expected requests and canned responses on a real client, and fails if calls differ.

from botocore.stub import Stubber
ec2 = boto3.client("ec2", region_name="us-east-1")
with Stubber(ec2) as st:
    st.add_response("describe_snapshots", {"Snapshots": [{"SnapshotId": "snap-1",
        "StartTime": datetime(2024, 1, 1, tzinfo=UTC), "VolumeSize": 8}]},
        {"OwnerIds": ["self"]})
    st.add_client_error("delete_snapshot", service_error_code="InvalidSnapshot.InUse")
    ...

Prefer it when you need exact control: specific error codes, throttling, odd fields, or asserting the exact parameters sent. moto is better for stateful flows (create then list).

All Python for DevOps questions Roadmap: Python for automation

More on Testing, services & metrics