# AssertionError: 未引发 ValueError

- **ID:** `python/unittest-assertraises-not-raised`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

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
   ```

## 无效尝试

- **** — This duplicates assertRaises and often leads to false positives if the exception is caught. (90% 失败率)
- **** — The underlying issue is that the exception is not raised, so switching frameworks won't help. (95% 失败率)
