我試圖模擬一個特定的boto3函數。我的模塊Cleanup導入boto3。清理也有一個班級,「清潔」。在初始化,清潔創建一個EC2的客戶:如何模擬一個boto3客戶端對象/調用
self.ec2_client = boto3.client('ec2')
我想嘲笑EC2客戶端的方法:desribe_tags(),這蟒蛇說的是:
<bound method EC2.describe_tags of <botocore.client.EC2 object at 0x7fd98660add0>>
我已經得到了最遠的是進口botocore在我的測試文件,並嘗試:
mock.patch(Cleaner.botocore.client.EC2.describe_tags)
其失敗:
AttributeError: 'module' object has no attribute 'EC2'
我該如何嘲笑這種方法?
清理看起來像:
import boto3
class cleaner(object):
def __init__(self):
self.ec2_client = boto3.client('ec2')
的ec2_client對象是具有desribe_tags()方法中的一個。這是一個botocore.client.EC2對象,但我從不直接導入botocore。
裏面的清理模塊。您究竟如何導入EC2來使用它?從它的外觀來看,你正在做類似'import boto3'的事情。對?所以,我會懷疑你的補丁應該是'Cleanup.boto3.EC2'。如果你命名你的模塊「清理」。一些更多的信息肯定會有所幫助。 – idjaw
添加模塊示例 –
@JeffTang您找到了解決方案嗎?我正在尋找類似的東西! – ptimson