简体   繁体   中英

Unable to do unit testing of Python AWS code

I have never done unit testing before, so I am not able to understand how to do it in any module of my code.

I have coded functons for tag insertion and deletion in AWS using boto3. I will attach the code of a function below. I need to know how to unit test the code without using mocking. Or is it even possible?

def tagging_insertion(self, source_objectname, tagset):
    s3_client = boto3.client('s3', region_name=self.parameter["default_region"])
    s3_client.put_object_tagging(
        Bucket = self.parameter["SourceBucketName"],
        Key = source_objectname,
        Tagging = tagset
    )

This is part of the code I want to unit test.

mocking the put_object_tagging is the right choice.

A "dirty" alternative is to do something like this, but it is not recommended . because the code should not be changed for tests. This is why mocks and fixtures are for.

def tagging_insertion(self, source_objectname, tagset, is_test=False):
     s3_client = boto3.client('s3', region_name=self.parameter["default_region"])
     if not is_test:
         s3_client.put_object_tagging(Bucket=self.parameter["SourceBucketName"],
                                      Key=source_objectname,Tagging=tagset)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM