英文:
How to properly use mock to test methods inside the except clause
问题
我有一个 Python 方法,应该尝试调用 get_item_dynamodb(),但如果有来自 boto3 的 ClientError,则应该调用其他 3 个方法。我想知道应该如何正确模拟 ClientError,然后测试 except 子句内的方法。
英文:
I have a python method which should try to call get_item_dynamodb(), but if there's a ClientError from boto3, then it should call the other 3 methods instead. I'm wondering how should I properly mock the ClientError and then test the methods inside the except clause.
main_method:
try:
get_item_dynamodb()
except ClientError:
start_athena_query()
get_query_result()
extract_info_from_result()
答案1
得分: 0
您可以使用一个具有side_effect
属性值为ClientError
异常的Mock
对象来对get_item_dynamodb
函数进行patch
,这样当调用get_item_dynamodb
函数时,它会引发一个ClientError
异常并进入异常处理程序块。
英文:
You can patch
the get_item_dynamodb
function with a Mock
object that has a side_effect
attribute with a value of a ClientError
exception, so that when the get_item_dynamodb
function is called it would raise a ClientError
exception and enter the exception handler block.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论