python runtime_error ai_generated true

AssertionError: 未引发 ValueError

AssertionError: ValueError not raised

ID: python/unittest-assertraises-not-raised

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-06-15首次发现

版本兼容性

版本状态引入弃用备注
3.8 active
3.12 active

根因分析

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

English

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

解决方案

  1. 85% 成功率
    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% 成功率
    with self.assertRaises(ValueError) as cm:
        my_func()
    print(cm.exception)  # see if any exception was raised

无效尝试

常见但无效的做法:

  1. 90% 失败

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

  2. 95% 失败

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