【发布时间】:2018-04-20 14:25:09
【问题描述】:
我正在使用unittest 测试一个使用boto3 调用AWS 的函数。
函数如下所示:
import boto3
def my_function():
client = boto3.client('athena')
res = client.start_query_exeuction(
QueryString='SELECT * FROM logs',
ResultConfiguration={'OutputLocation': 's3://mybucket'}
)
return res['QueryExecutionId']
我在我的单元测试中使用 botocore 存根来存根这个请求,如下所示:
from botocore.stub import Stubber
import botocore.session
def test_my_function():
client = botocore.session.get_session().create_client('athena')
client_res = {'QueryExecutionId': 'testid'}
exp_params = {
'QueryString': 'SELECT * FROM logs',
'ResultConfiguration': {
'OutputLocation': 's3://mybucket'
}
}
with Stubber(client) as stubber:
stubber.add_response('start_query_execution', client_res, exp_params)
res = my_function()
self.assertEqual(res, 'testid')
这个测试失败了
botocore.exceptions.ClientError:发生错误 (UnrecognizedClientException) 调用 StartQueryExecution 时 operation: 请求中包含的安全令牌无效。
为什么会失败?是因为我在my_function() 中创建了一个与存根中使用的客户端不同的新客户端吗?如果是这样,我该如何测试?
非常感谢任何帮助。
【问题讨论】:
标签: python boto3 python-unittest botocore