# 断言错误：未引发异常

- **ID:** `python/assertionerror-assert-raises`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

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

## 版本兼容性

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

## 解决方案

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

## 无效尝试

- **Adding a try-except block manually** — Manual try-except may miss the exact exception type or not fail properly. (60% 失败率)
- **Using a broader exception type** — Catches unintended exceptions and may hide bugs. (50% 失败率)
