【问题标题】:Expects Raise Error does not handle negative unit test期望引发错误不处理负单元测试
【发布时间】:2022-01-27 23:22:47
【问题描述】:

我遇到了一个基本否定单元测试的问题。尝试在最后一个函数中成功预期 NotFoundException 错误。 我遇到的问题是堆栈首先捕获错误并完全取消该 expect() 函数。请帮忙,TIA。

‘’’

def test_should_return_coffee_not_found(
    coffee_information_repository_mock, coffee_information, coffee_drink
):

    coffee_information_repository_mock.get_coffee_information.return_value = (
        coffee_information
    )

    coffee_information_service = CoffeeInformationService(
        coffee_information_repository_mock
    )

    result = coffee_information_service.get_drink_by_title("Gatorade")

    expect(result).to(raise_error(NotFoundException))
    

‘’’

【问题讨论】:

    标签: python unit-testing assertion


    【解决方案1】:

    expects 的引发错误匹配器要求您将无参数函数传递给expect。您没有传递期望的回调,而是调用要测试的函数。最简单的方法是使用 lambda

    expect(lambda: coffee_information_service.get_drink_by_title("Gatorade")).to(raise_error(NotFoundException))
    

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2019-02-04
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多