How to mock a boto3 client object/call

后端 未结 2 693
别那么骄傲
别那么骄傲 2021-02-14 20:31

I\'m trying to mock one particular boto3 function. My module, Cleanup, imports boto3. Cleanup also has a class, \"cleaner\". During init, cleaner creates an ec2 client:

相关标签:
2条回答
  • 2021-02-14 21:13

    I found a solution to this when trying to mock a different method for the S3 client

    import botocore
    from mock import patch
    import boto3
    
    orig = botocore.client.BaseClient._make_api_call
    
    def mock_make_api_call(self, operation_name, kwarg):
        if operation_name == 'DescribeTags':
            # Your Operation here!
            print(kwarg)
        return orig(self, operation_name, kwarg)
    
    with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call):
        client = boto3.client('ec2')
        # Calling describe tags will perform your mocked operation e.g. print args
        e = client.describe_tags()
    

    Hope it helps :)

    0 讨论(0)
  • 2021-02-14 21:26

    You should be mocking with respect to where you are testing. So, if you are testing your cleaner class (Which I suggest you use PEP8 standards here, and make it Cleaner), then you want to mock with respect to where you are testing. So, your patching should actually be something along the lines of:

    class SomeTest(Unittest.TestCase):
        @mock.patch('path.to.Cleaner.boto3.client', return_value=Mock())
        def setUp(self, boto_client_mock):
            self.cleaner_client = boto_client_mock.return_value
    
        def your_test(self):
            # call the method you are looking to test here
    
            # simple test to check that the method you are looking to mock was called
            self.cleaner_client.desribe_tags.assert_called_with()
    

    I suggest reading through the mocking documentation which has many examples to do what you are trying to do

    0 讨论(0)
提交回复
热议问题