Skip to main content

Records AWS boto calls and generates unit tests

Project description

botocore stubber recorder

This library will allow you to record all the AWS API calls in a session, and generate a unittest.

Within the botocore library a Stubber is provided. The stubber allows you to create unittest for pieces of code which call AWS APIs. The stub will act as an endpoint, returning the appropriate response to each request in order.

However, using the stubs in your unittest is quite laborious. You have to record the requests and responses, configure the stub and run the test. This library makes it pretty simple.

how to record all AWS API calls?

To record all AWS API calls is really simple. Just add the following snippet:

import boto3
from botocore_stubber_recorder import BotoRecorder

session = boto3.session.Session()
recorder = BotoRecorder(session)
# ... do your thing with the session
for index, call in enumerate(recorder.calls):
   print(f"call {index +1}: {call.service_name}:{call.operation}")

Generating a unittest

To generate a unittest, add the following snippet:

from botocore_stubber_recorder import UnitTestGenerator

generator = UnitTestGenerator(name="my_example",directory="./tests")
generator.generate(recorder, anonimize=True)

This will generate the following file structure:

└── my_example
    ├── call_00001_describe_regions
    │   └──
    ├── call_00002_describe_parameters
    │   └──
    ├── call_00003_...
    │   └──

The contains a base unittest class which initializes the stub with all the recorded calls. Note that the and the call directories are overwritten on each generate request. The contains an example unittest implementation, which needs to be changed to contain the actual test. The generated test just tests that the generated stub: you have to replace the method test_my_example with a functional test.

Run the generated test

You can now run, the generated test:

cd tests
python -munittest tests/my_example/
WARNING:root:TODO: replace MyExampleUnitTest.test_my_example with the actual test. This just tests the generated code.
Ran 1 test in 0.092s


Now, edit the test in tests/my_example/ to implement the actual unittest.

all at once

To record and generated the unittest in a single command, use:

import boto3
from botocore_stubber_recorder import BotoRecorderUnitTestGenerator

session = boto3.session.Session()
with BotoRecorderUnitTestGenerator("my_example", session) as generator:
    ## do your thing with the session

generated code format

The generated code is formatted using black, if black is on the path.

Project details

Download files

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

Source Distribution

botocore-stubber-recorder-0.4.1.tar.gz (8.8 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page