代码之家  ›  专栏  ›  技术社区  ›  muon

pytest.raises传递的异常错误

  •  -1
  • muon  · 技术社区  · 7 年前

    在下面的示例中,当 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
    

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   jwodder    7 年前

    你的测试对 expecting . 你写 with pytest.raises(ValueError): ,所以pytest总是在寻找 ValueError 这是什么 fun() 加薪。也许你想写 with pytest.raises(expecting): 相反?