Python 如何使用pytest模拟boto3自定义类方法?
我不确定是否应该使用moto、Stubber或修补模拟类,因为我的类是定制的。我应该如何为下面的类编写测试 aws.py: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):
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应该可以很好地模仿这一点