Package: botocore-stubber-recorder
botocore stubber recorderThis library will allow you to record all the AWS API calls in a session, and generate a unittest.Within the botocore library aStubberis 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.getting startedTo start, type:$ botocore-stubber-recorder --test-name show_me_how
INFO:root:sketelon test sts_get_caller written in ./testsThis generates the following files:$ tree tests 
tests
├── __init__.py
└── show_me_how
    ├── __init__.py
    ├── base.py
    ├── call_00001_get_caller_identity
    │   └── __init__.py
    └── test_show_me_how.pyYou can now start writing your unit test intest_sts_get_caller.py. By running
the unit test withself.record = Trueor the environment variableRECORD_UNITTEST_STUBS=trueit will re-record the stubs for you.how to record all AWS API calls?To record all AWS API calls is really simple. Just add the following snippet:importboto3frombotocore_stubber_recorderimportBotoRecordersession=boto3.session.Session()recorder=BotoRecorder(session)# ... do your thing with the sessionforindex,callinenumerate(recorder.calls):print(f"call{index+1}:{call.service_name}:{call.operation}")Generating a unittestTo generate a unittest, add the following snippet:frombotocore_stubber_recorderimportUnitTestGeneratorgenerator=UnitTestGenerator(name="my_example",directory="./tests")generator.generate(recorder,anonimize=True,unflatten=True)This will generate the following file structure:tests
└── my_example
    ├── __init__.py
    ├── base.py
    ├── call_00001_describe_regions
    │   └── __init__.py
    ├── call_00002_describe_parameters
    │   └── __init__.py
    ├── call_00003_...
    │   └── __init__.py
    └── test_my_example.pyThebase.pycontains a base unittest class which initializes the stub with all the recorded
calls. Note that thebase.pyand the call directories are overwritten on each generate request.
Thetest_my_example.pycontains 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 methodtest_my_examplewith a functional test.Run the generated testYou can now run, the generated test:cdtests
python-munittest./my_example/test_my_example.pyWARNING:root:TODO: replace MyExampleUnitTest.test_my_example with the actual test. This just tests the generated code.
.
----------------------------------------------------------------------
Ran 1 test in 0.092s

OKNow, edit the test intests/my_example/test_my_example.pyto implement the actual unittest.recording the responsesIf you want to re-record the AWS API stubs, set the environment variable RECORD_UNITTEST_STUBS totrue.RECORD_UNITTEST_STUBS=true python -munittest ./my_example/test_my_example.pyThis will allow you to change the implementation of the unit test, and easily record
the calls.all at onceTo record and generated the unittest in a single command, use:importboto3frombotocore_stubber_recorderimportBotoRecorderUnitTestGeneratorsession=boto3.session.Session()withBotoRecorderUnitTestGenerator("my_example",session)asgenerator:## do your thing with the sessiongenerated code formatThe generated code is formatted using black, ifblackis on the path.