Skip to main content

Stub boto3 clients to avoid hitting real AWS endpoints in tests

Project description

Stuboto

Stub boto3 clients to avoid hitting real AWS endpoints in tests.

Usage

You can use Stuboto instances as if they were botocore.stub.Stubber:

import boto3
from stuboto import Stuboto

def test_create_bucket():
    s3 = boto3.client("s3")
    stubber = Stuboto(s3)
    stubber.activate()

    response = {"Location": "my-bucket"}
    expected_params = {"Bucket": "my-bucket", "ACL": "private"}

    stubber.add_response("create_bucket", response, expected_params)
    service_response = s3.create_bucket(Bucket="my-bucket", ACL="private")
    assert service_response == response

Stuboto instances are decorated with all the same methods as the original boto3 client so you can also stub responses with the arguably more readable:

import boto3
from stuboto import Stuboto

def test_create_bucket():
    s3 = boto3.client("s3")
    stubber = Stuboto(s3)
    stubber.activate()

    response = stubber.create_bucket(Bucket="my-bucket", ACL="private").add_response(
        Location="my-bucket"
    )
    service_response = s3.create_bucket(Bucket="my-bucket", ACL="private")
    assert service_response == response

botocore.stub.Stubber documentation suggests:

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.

But I won't tell anybody if you leave some values out in favor of writing more readable tests.

Installation

pip install stuboto

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for stuboto, version 0.3
Filename, size File type Python version Upload date Hashes
Filename, size stuboto-0.3-py3-none-any.whl (3.1 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size stuboto-0.3.tar.gz (2.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page