Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pytest模拟boto3自定义类方法?_Python_Python 3.x_Mocking_Boto3 - Fatal编程技术网

Python 如何使用pytest模拟boto3自定义类方法?

Python 如何使用pytest模拟boto3自定义类方法?,python,python-3.x,mocking,boto3,Python,Python 3.x,Mocking,Boto3,我不确定是否应该使用moto、Stubber或修补模拟类,因为我的类是定制的。我应该如何为下面的类编写测试 aws.py: import boto3 class MyAWSClass: def __init__(self, profile_name): self.session = boto3.Session(profile_name=profile_name) def get_instances(self, region_name):

我不确定是否应该使用moto、Stubber或修补模拟类,因为我的类是定制的。我应该如何为下面的类编写测试

aws.py:

import boto3
    

class MyAWSClass:
    def __init__(self, profile_name):
        self.session = boto3.Session(profile_name=profile_name)
    
    def get_instances(self, region_name):
        """Returns a list of instances"""
        client = self.session.client("ec2", region_name=region_name)
        reservations = client.describe_instances()["Reservations"]
        instances = []
        for reservation in reservations:
            instances.extend(reservation["Instances"])
        return instances

    def create_tags(self, dry_run, resources, tags, region_name):
        """Create tags"""
        client = self.session.client("ec2", region_name=region_name)
        response = client.create_tags(DryRun=dry_run, Resources=resources, Tags=tags)
        return response

Moto应该可以很好地模仿这一点