# AssertionError：parse_input 未引发 ValueError

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

## 根因

assertRaises 期望可调用对象引发指定的异常。如果函数正常返回（例如它已被修复，或者输入有效），上下文管理器的 __exit__ 不会收到异常，测试就会失败。

## 版本兼容性

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

## 解决方案

1. **** (97% 成功率)
   ```
   def parse_input(s):
    if not s:
        raise ValueError('empty input')
    return int(s)

# test
with self.assertRaises(ValueError):
    parse_input('')
   ```
2. **** (96% 成功率)
   ```
   import pytest

def test_parse_input_empty():
    with pytest.raises(ValueError, match='empty input'):
        parse_input('')
   ```

## 无效尝试

- **** — assertRaisesRegex still requires the exception to be raised; an empty pattern matches anything but the exception absence still fails. (90% 失败率)
- **** — Reimplements assertRaises worse; loses the informative 'ValueError not raised' message and complicates the test. (70% 失败率)
