# 断言错误：在子测试（index=0）中 2 != 3

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

## 根因

subTest上下文管理器因断言错误而失败，表明循环测试的一次迭代失败，而其他迭代可能通过。

## 版本兼容性

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

## 解决方案

1. **Debug the specific subTest iteration that fails** (90% 成功率)
   ```
   for i, value in enumerate(data):
    with self.subTest(i=i):
        self.assertEqual(value, expected[i])
If it fails, check the value and expected at index i.
   ```
2. **Use a more descriptive subTest message to identify the failing case** (85% 成功率)
   ```
   with self.subTest(i=i, value=value):
    self.assertEqual(value, expected[i])
This provides more context in the error message.
   ```

## 无效尝试

- **Removing the subTest context to run all iterations as separate tests** — This changes the test structure and may not capture the context of the failure. (50% 失败率)
- **Increasing the number of iterations to avoid the specific failing case** — This masks the bug; the failing case should be fixed. (80% 失败率)
