python runtime_error ai_generated true

AssertionError: ValueError not raised

ID: python/unittest-assertraises-not-raised

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.12 active

Root Cause

The code under test does not raise the expected exception, or the exception is caught inside the function, or the test is not actually executing the code that should raise.

generic

中文

被测代码没有引发预期的异常,或者异常在函数内部被捕获,或者测试实际上没有执行应该引发异常的代码。

Workarounds

  1. 85% success
    Add print statements before the call to ensure it's reached. Use pdb.set_trace() to step through. Check if the function has a try/except that swallows the error.
  2. 90% success
    with self.assertRaises(ValueError) as cm:
        my_func()
    print(cm.exception)  # see if any exception was raised

Dead Ends

Common approaches that don't work:

  1. 90% fail

    This duplicates assertRaises and often leads to false positives if the exception is caught.

  2. 95% fail

    The underlying issue is that the exception is not raised, so switching frameworks won't help.