Stubber Reference#

botocore.stub#

class botocore.stub.Stubber(client)#

This class will allow you to stub out requests so you don’t have to hit an endpoint to write tests. Responses are returned first in, first out. If operations are called out of order, or are called with no remaining queued responses, an error will be raised.

Example:

import datetime
import botocore.session
from botocore.stub import Stubber


s3 = botocore.session.get_session().create_client('s3')
stubber = Stubber(s3)

response = {
    'IsTruncated': False,
    'Name': 'test-bucket',
    'MaxKeys': 1000, 'Prefix': '',
    'Contents': [{
        'Key': 'test.txt',
        'ETag': '"abc123"',
        'StorageClass': 'STANDARD',
        'LastModified': datetime.datetime(2016, 1, 20, 22, 9),
        'Owner': {'ID': 'abc123', 'DisplayName': 'myname'},
        'Size': 14814
    }],
    'EncodingType': 'url',
    'ResponseMetadata': {
        'RequestId': 'abc123',
        'HTTPStatusCode': 200,
        'HostId': 'abc123'
    },
    'Marker': ''
}

expected_params = {'Bucket': 'test-bucket'}

stubber.add_response('list_objects', response, expected_params)
stubber.activate()

service_response = s3.list_objects(Bucket='test-bucket')
assert service_response == response

This class can also be called as a context manager, which will handle activation / deactivation for you.

Example:

import datetime
import botocore.session
from botocore.stub import Stubber


s3 = botocore.session.get_session().create_client('s3')

response = {
    "Owner": {
        "ID": "foo",
        "DisplayName": "bar"
    },
    "Buckets": [{
        "CreationDate": datetime.datetime(2016, 1, 20, 22, 9),
        "Name": "baz"
    }]
}


with Stubber(s3) as stubber:
    stubber.add_response('list_buckets', response, {})
    service_response = s3.list_buckets()

assert service_response == response

If you have an input parameter that is a randomly generated value, or you otherwise don’t care about its value, you can use stub.ANY to ignore it in validation.

Example:

import datetime
import botocore.session
from botocore.stub import Stubber, ANY


s3 = botocore.session.get_session().create_client('s3')
stubber = Stubber(s3)

response = {
    'IsTruncated': False,
    'Name': 'test-bucket',
    'MaxKeys': 1000, 'Prefix': '',
    'Contents': [{
        'Key': 'test.txt',
        'ETag': '"abc123"',
        'StorageClass': 'STANDARD',
        'LastModified': datetime.datetime(2016, 1, 20, 22, 9),
        'Owner': {'ID': 'abc123', 'DisplayName': 'myname'},
        'Size': 14814
    }],
    'EncodingType': 'url',
    'ResponseMetadata': {
        'RequestId': 'abc123',
        'HTTPStatusCode': 200,
        'HostId': 'abc123'
    },
    'Marker': ''
}

expected_params = {'Bucket': ANY}
stubber.add_response('list_objects', response, expected_params)

with stubber:
    service_response = s3.list_objects(Bucket='test-bucket')

assert service_response == response
activate()#

Activates the stubber on the client

add_client_error(method, service_error_code='', service_message='', http_status_code=400, service_error_meta=None, expected_params=None, response_meta=None, modeled_fields=None)#

Adds a ClientError to the response queue.

Parameters:
  • method (str) – The name of the service method to return the error on.

  • service_error_code (str) – The service error code to return, e.g. NoSuchBucket

  • service_message (str) – The service message to return, e.g. ‘The specified bucket does not exist.’

  • http_status_code (int) – The HTTP status code to return, e.g. 404, etc

  • service_error_meta (dict) – Additional keys to be added to the service Error

  • expected_params – A dictionary of the expected parameters to be called for the provided service response. The parameters match the names of keyword arguments passed to that client call. If any of the parameters differ a StubResponseError is thrown. You can use stub.ANY to indicate a particular parameter to ignore in validation.

  • response_meta (dict) – Additional keys to be added to the response’s ResponseMetadata

  • modeled_fields (dict) – Additional keys to be added to the response based on fields that are modeled for the particular error code. These keys will be validated against the particular error shape designated by the error code.

add_response(method, service_response, expected_params=None)#

Adds a service response to the response queue. This will be validated against the service model to ensure correctness. It should be noted, however, that while missing attributes are often considered correct, your code may not function properly if you leave them out. Therefore you should always fill in every value you see in a typical response for your particular request.

Parameters:
  • method (str) – The name of the client method to stub.

  • service_response (dict) – A dict response stub. Provided parameters will be validated against the service model.

  • expected_params – A dictionary of the expected parameters to be called for the provided service response. The parameters match the names of keyword arguments passed to that client call. If any of the parameters differ a StubResponseError is thrown. You can use stub.ANY to indicate a particular parameter to ignore in validation. stub.ANY is only valid for top level params.

assert_no_pending_responses()#

Asserts that all expected calls were made.

deactivate()#

Deactivates the stubber on the client