# AssertionError: 未引发 ValueError

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

## 根因

self.assertRaises(ValueError) 作为上下文管理器使用，但被测代码未引发异常，或异常在函数内部被捕获并吞掉。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   with self.assertRaises(ValueError) as ctx:
    parse('bad-input')
self.assertIn('bad-input', str(ctx.exception))
   ```
2. **** (92% 成功率)
   ```
   import pytest

def test_parse_raises():
    with pytest.raises(ValueError, match=r'bad-input'):
        parse('bad-input')
   ```

## 无效尝试

- **** — Inverts the logic: except catches the expected exception and calls fail, so a correct raise now fails the test. (85% 失败率)
- **** — Broadens the assertion so much that any unrelated error (e.g. TypeError) passes the test, masking real bugs. (75% 失败率)
