python runtime_error ai_generated true

AssertionError: Exception not raised

ID: python/assertionerror-assert-raises

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-12-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active
3.10 active

Root Cause

A test expects an exception to be raised using pytest.raises or assertRaises, but the code executes without exception.

generic

中文

测试期望使用 pytest.raises 或 assertRaises 引发异常,但代码执行时未引发异常。

Workarounds

  1. 95% success Use pytest.raises with exact exception
    with pytest.raises(ValueError):
        raise ValueError('test')
  2. 90% success Use unittest.TestCase.assertRaises
    self.assertRaises(ValueError, func, arg)

Dead Ends

Common approaches that don't work:

  1. Adding a try-except block manually 60% fail

    Manual try-except may miss the exact exception type or not fail properly.

  2. Using a broader exception type 50% fail

    Catches unintended exceptions and may hide bugs.