Package: boto3-mocking
Boto3-Mocking - Centralized Mocking of Boto3 for TestingWhen testing code that accesses AWS through theboto3library, it is often
desirable to isolate the code from actual access to the AWS API.  This library
facilitates both that and dispatch of the service requests to mock
implementations.Install$pipinstallboto3-mockingUsageEngaging the PatchesTwo options exist to engage the patching ofboto3: permanent and contextual.  To permanently patchboto3within the process, somewhere in the testing code that precedes use of theboto3.clientandboto3.resourcefunctions, use this code:importboto3_mockingboto3_mocking.engage_patching()NOTEIf your mainline code constructsboto3clients or resources when it loads, then it is critical for the test harness to engage patching before loading mainline code.To temporarily patchboto3, use this code:importboto3_mockingwithboto3_mocking.patching:...yourcodeaccessingAWShere...Testing for Patch EngagementThis package exposes a function to test whether patching is currently engaged.  Use this in your testing code before doing something that would harm or be expensive on the actual AWS environment:importboto3_mockingifnotboto3_mocking.patching_engaged():raiseException("...")Registering a Service HandlerService handlers can either be registered permanently or contextually.  Registration is managed throughboto3_mocking.clientsandboto3_mocking.resources, corresponding toboto3.clientandboto3.resource.  A service handler (whether for a client or a resource) is a callable that accepts arguments by keyword and returns the mock client or resource as appropriate.  The handler should accept the same keywords as the client or service it substitutes.  Registering a mock clientdoes notautomatically mock a corresponding resource, as the resource does not useboto3.clientto construct/obtain its client object.To permanently register a client handler for the'cognito-idp'service, the code would look like:importboto3_mockingboto3_mocking.clients.register_handler('cognito-idp',MockCognitoIdpClient)Handlers can also be contextually registered:importboto3_mockingwithboto3_mocking.resources.handler_for('ec2',MockEC2Resource):...yourcodeaccessingEC2here...Only one handler may be permanently registered for each service, but a contextually registered handler registration does override a permanently registered one for the same service during its context without error or warning.Accessing Real Boto3 Functionalityboto3_mocking.clientsandboto3_mocking.resourcesboth provide arealattribute holding the original function fromboto3from before the patching is engaged.  Your service handlers can make use of these functions if necessary.  One example would be to redirect'dynamodb'clients and resources to a DynamoDBLocal instance, where the actualboto3library is still needed but needs to have certain keyword arguments (endpoint_urlanduse_ssl) included when the client or resource is created.The Allowed ListsAccess to the real functionality ofboto3can be allowed service-by-service in bothboto3_mocking.clientsandboto3_mocking.resourcesvia theirallowedset.  Justaddthe name of the service toboto3_mocking.clients.allowedorboto3_mocking.resources.allowedto allow those calls without a registered handler.Access to Unhandled ServicesIf a service client or resource is accessed without a handler,boto3_mockingraises aboto3_mocking.UnpatchedAccessexception unless the service is in the respectiveallowedlist.LicenseThis code is licensed under Apache License 2.0.  Please seeLICENSEfor full text.ContributingFork it on GitHub (https://github.com/rtweeks/boto3-mocking)Create your feature branch (git checkout -b my-new-feature)Commit your changes (git commit -am 'Add some feature')Push to the branch (git push origin my-new-feature)Create a new Pull Request (onGitHub)