# 断言错误：断言 'key' 在 {'a': 1} 中

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

## 根因

键不在字典中，导致 'in' 运算符返回 False。

## 版本兼容性

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

## 解决方案

1. **Add the key to the dictionary before assertion** (95% 成功率)
   ```
   d = {'a': 1}; d['key'] = 'value'; assert 'key' in d
   ```
2. **Use assertIn for better error messages** (90% 成功率)
   ```
   self.assertIn('key', {'a': 1})
   ```

## 无效尝试

- **Checking with .get() and ignoring None** — .get() returns None for missing keys, which may mask the absence. (60% 失败率)
- **Using assertEqual with dict.keys()** — dict.keys() returns a view; comparison may be misleading. (40% 失败率)
