# AssertionError: <class 'ValueError'> 未引发

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

## 根因

被测代码未引发预期的异常。这可能是因为代码路径未触发、异常类型不同，或代码静默处理了错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (92% 成功率)
   ```
   with self.assertRaises(ValueError) as cm:
    my_function(invalid_input)
self.assertEqual(str(cm.exception), 'expected message')
   ```
2. **** (95% 成功率)
   ```
   import pytest

with pytest.raises(ValueError, match='expected message'):
    my_function(invalid_input)
   ```

## 无效尝试

- **** — This broadens the assertion and may pass even when the wrong exception is raised, hiding bugs. (80% 失败率)
- **** — This bypasses assertRaises entirely and does not verify the exception type correctly. (85% 失败率)
