python
config_error
ai_generated
true
ValueError: id 数量不正确:3 而不是 2
ValueError: Incorrect number of ids: 3 instead of 2
ID: python/pytest-mark-parametrize-ids-mismatch
80%修复率
87%置信度
0证据数
2024-02-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
在 @pytest.mark.parametrize 中,`ids` 列表长度必须与参数集的数量匹配(而不是参数个数)。提供过多或过少的 id 会引发 ValueError。
English
In @pytest.mark.parametrize, the `ids` list length must match the number of parameter sets (not the number of arguments). Supplying too many or too few ids raises ValueError.
解决方案
-
98% 成功率
@pytest.mark.parametrize( 'a,b', [(1, 2), (3, 4)], ids=['small', 'large'], # 2 ids for 2 tuples ) def test_add(a, b): ... -
90% 成功率
def idfn(val): return f'v={val}' @pytest.mark.parametrize('x', [1, 2, 3], ids=idfn) def test_x(x): ...
无效尝试
常见但无效的做法:
-
85% 失败
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.
-
30% 失败
Works but loses readable test names; not a real fix if the user needs custom ids for reporting.