【问题标题】:pytest.raises passes with wrong exceptionpytest.raises 通过错误异常
【发布时间】:2018-10-17 15:34:42
【问题描述】:

在下面的示例中,我预计当 expecting = NotImplementedError 时测试会失败。

import pytest
def fun():
    raise ValueError()

@pytest.mark.parametrize("expecting", [
    (ValueError),
    (NotImplementedError)
])
def test_something( expecting):
    with pytest.raises(ValueError):
        fun()

但是,它通过了:

test_something[ValueError] PASSED
test_something[NotImplementedError] PASSED

为什么会出现这种行为,正确的用法是什么?

【问题讨论】:

  • 哇,这个社区太残酷了!

标签: python unit-testing testing automated-tests pytest


【解决方案1】:

您的测试对expecting 没有任何作用。你写了with pytest.raises(ValueError):,所以pytest一直在寻找ValueError,这就是fun()提出的。也许您打算改为写with pytest.raises(expecting):

【讨论】:

  • 谢谢我完全错过了.. 我正在关注 pytest 示例
猜你喜欢
  • 2017-05-30
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 2020-11-03
  • 2021-06-25
  • 1970-01-01
  • 2015-05-26
相关资源
最近更新 更多