# ValueError: id 数量不正确：3 而不是 2

- **ID:** `python/pytest-mark-parametrize-ids-mismatch`
- **领域:** python
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 @pytest.mark.parametrize 中，`ids` 列表长度必须与参数集的数量匹配（而不是参数个数）。提供过多或过少的 id 会引发 ValueError。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (98% 成功率)
   ```
   @pytest.mark.parametrize(
    'a,b',
    [(1, 2), (3, 4)],
    ids=['small', 'large'],  # 2 ids for 2 tuples
)
def test_add(a, b):
    ...
   ```
2. **** (90% 成功率)
   ```
   def idfn(val):
    return f'v={val}'

@pytest.mark.parametrize('x', [1, 2, 3], ids=idfn)
def test_x(x):
    ...
   ```

## 无效尝试

- **** — pytest expects an iterable of id strings (or a callable); a single string is treated as an iterable of characters, producing confusing ids and possibly a length mismatch. (85% 失败率)
- **** — Works but loses readable test names; not a real fix if the user needs custom ids for reporting. (30% 失败率)
