相关疑难解决方法(0)

如何模拟boto3客户端对象/调用

我正在尝试模拟一个特定的boto3函数.我的模块Cleanup导入boto3.清理也有一个"清洁"类.在init期间,cleaner创建了一个ec2客户端:

self.ec2_client = boto3.client('ec2')
Run Code Online (Sandbox Code Playgroud)

我想模拟ec2客户端方法:desribe_tags(),python说:

<bound method EC2.describe_tags of <botocore.client.EC2 object at 0x7fd98660add0>>
Run Code Online (Sandbox Code Playgroud)

我得到的最远的是在我的测试文件中导入botocore并尝试:

mock.patch(Cleaner.botocore.client.EC2.describe_tags)
Run Code Online (Sandbox Code Playgroud)

失败的是:

AttributeError: 'module' object has no attribute 'EC2'
Run Code Online (Sandbox Code Playgroud)

我该如何模仿这种方法?

清理看起来像:

import boto3
class cleaner(object):
    def __init__(self):
        self.ec2_client = boto3.client('ec2')
Run Code Online (Sandbox Code Playgroud)

ec2_client对象是具有desribe_tags()方法的对象.它是一个botocore.client.EC2对象,但我从不直接导入botocore.

python unit-testing mocking

6
推荐指数
2
解决办法
5782
查看次数

标签 统计

mocking ×1

python ×1

unit-testing ×1