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).
More on Testing, services & metrics
Why should automation scripts have tests at all?BeginnerHow does pytest discover and run tests?BeginnerWhy does pytest use plain `assert` instead of `assertEqual`?BeginnerWhat are pytest fixtures, and what scopes exist?IntermediateWhat built-in fixtures do you use most?IntermediateHow does `@pytest.mark.parametrize` work?Beginner