gpt4 book ai didi

python-2.7 - aws boto3 客户端 Stubber 帮助 stub 单元测试

转载 作者:行者123 更新时间:2023-12-05 03:08:14 28 4
gpt4 key购买 nike

我正在尝试为 aws RDS 编写一些单元测试。目前,启动停止rds api调用尚未在moto中实现。我试着模拟 boto3,但遇到了各种奇怪的问题。我做了一些谷歌搜索,发现 http://botocore.readthedocs.io/en/latest/reference/stubber.html

所以我尝试为 rds 实现该示例,但代码似乎表现得像普通客户端,即使我已将其 stub 。不确定发生了什么或者我是否正确 stub ?

from LambdaRdsStartStop.lambda_function import lambda_handler
from LambdaRdsStartStop.lambda_function import AWS_REGION

def tests_turn_db_on_when_cw_event_matches_tag_value(self, mock_boto):
client = boto3.client('rds', AWS_REGION)
stubber = Stubber(client)
response = {u'DBInstances': [some copy pasted real data here], extra_info_about_call: extra_info}
stubber.add_response('describe_db_instances', response, {})

with stubber:
r = client.describe_db_instances()
lambda_handler({u'AutoStart': u'10:00:00+10:00/mon'}, 'context')

因此,stubber 中第一行的模拟工作正常,r 的值作为我的 stub 数据返回。当我尝试进入我的 lambda_function.py 中的 lambda_handler 方法并仍然使用 stub 客户端时,它的行为就像一个普通的未 stub 客户端:

lambda_函数.py

def lambda_handler(event, context):
rds_client = boto3.client('rds', region_name=AWS_REGION)
rds_instances = rds_client.describe_db_instances()

错误输出:

  File "D:\dev\projects\virtual_envs\rds_sloth\lib\site-packages\botocore\auth.py", line 340, in add_auth
raise NoCredentialsError
NoCredentialsError: Unable to locate credentials

最佳答案

您需要在要测试的例程中调用 boto3 的地方修补它。此外,每次调用都会消耗 Stubber 响应,因此每个 stub 调用都需要另一个 add_response,如下所示:

def tests_turn_db_on_when_cw_event_matches_tag_value(self, mock_boto):
client = boto3.client('rds', AWS_REGION)
stubber = Stubber(client)
# response data below should match aws documentation otherwise more errors due to botocore error handling
response = {u'DBInstances': [{'DBInstanceIdentifier': 'rds_response1'}, {'DBInstanceIdentifierrd': 'rds_response2'}]}

stubber.add_response('describe_db_instances', response, {})
stubber.add_response('describe_db_instances', response, {})

with mock.patch('lambda_handler.boto3') as mock_boto3:
with stubber:
r = client.describe_db_instances() # first_add_response consumed here
mock_boto3.client.return_value = client
response=lambda_handler({u'AutoStart': u'10:00:00+10:00/mon'}, 'context') # second_add_response would be consumed here
# asert.equal(r,response)

关于python-2.7 - aws boto3 客户端 Stubber 帮助 stub 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45604467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com