python runtime_error ai_generated true

Failed: DID NOT RAISE <class 'ValueError'>

ID: python/pytest-assert-raises-no-exception

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2025-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active

Root Cause

使用pytest.raises期望抛出特定异常,但代码没有抛出,导致测试失败。

generic

中文

pytest.raises期望异常,但代码未抛出,测试失败。

Workarounds

  1. 95% success
    def func():
        raise ValueError('invalid')
    with pytest.raises(ValueError):
        func()
  2. 90% success
    with pytest.raises(ValueError, match='invalid'):
        func()

Dead Ends

Common approaches that don't work:

  1. 90% fail

    掩盖了代码逻辑错误。

  2. 70% fail

    与pytest.raises冲突,且不必要。