# 断言错误：调用 func() 时未引发 ValueError

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

## 根因

测试使用了 assertRaises，但被测试代码没有引发预期的异常，可能是由于代码中的错误或测试设置不正确。

## 版本兼容性

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

## 解决方案

1. **** (85% 成功率)
   ```
   Debug the code under test to ensure it raises the exception under the given conditions. For example, check input validation logic.
   ```
2. **** (90% 成功率)
   ```
   Use assertRaises with a lambda or callable: self.assertRaises(ValueError, func, arg) to ensure the exception is raised during the call.
   ```

## 无效尝试

- **** — Wrapping the call in a try-except block and manually asserting may mask other issues like incorrect exception types. (60% 失败率)
- **** — Changing the expected exception to a broader type (e.g., Exception) works but reduces test specificity and may hide bugs. (40% 失败率)
