# AssertionError: my_function 未引发 ValueError

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

## 根因

被测函数未引发预期的异常，或者异常在函数内部被捕获且未重新引发。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   def my_function(x):
    if x < 0:
        raise ValueError('negative')
    return x

with self.assertRaises(ValueError):
    my_function(-1)
   ```
2. **** (90% 成功率)
   ```
   with pytest.raises(ValueError, match='negative'):
    my_function(-1)
   ```

## 无效尝试

- **** — Tests the test, not the function; false positive. (95% 失败率)
- **** — Too broad; may pass for unrelated exceptions and hide bugs. (70% 失败率)
