如何正确使用模拟来测试`except`子句内的方法。

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月9日 12:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定