首页 > 解决方案 > Stubber 没有 boto3 属性

问题描述

    self.cf_client = boto3.client('cloudformation')
    self.cf_client_stubber = Stubber(self.cf_client)

    print(f" cc {type(self.cf_client_stubber)}")

    self.cf_client_stubber.add_response("describe_stacks", self.cf_describe_response,
                                        self.cf_describe_params)
    self.cf_client_stubber.activate()

    stackList = self.cf_client_stubber.describe_stacks(StackName=self.cf_stack_name, NextToken='xyz')

   

每当我使用 pytest 运行此方法时,都会遇到以下错误:

E           AttributeError: 'Stubber' object has no attribute 'describe_stacks'

python/src/example.py:26: AttributeError

关于主要问题是什么的任何见解?

标签: amazon-web-servicesamazon-cloudformationboto3stub

解决方案


我已经找到了解决这个问题的方法。

  1. 当你调用方法时,不要使用存根。使用实际的 boto3 客户端。
  2. 将存根方法按照您正在测试的方法的顺序放置,您将调用类似的方法add_reponse。例如,如果describe_stacks调用 before delete_stack,则在测试中添加describe_stacksbefore delete_stack;否则,将找不到方法。

推荐阅读