python
config_error
ai_generated
true
值错误:重复的测试ID:'test_case_0'
ValueError: duplicate test IDs: 'test_case_0'
ID: python/pytest-parametrize-ids-duplicate
80%修复率
85%置信度
0证据数
2025-08-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.0.0 | active | — | — | — |
| 8.0.0 | active | — | — | — |
根因分析
当使用pytest.mark.parametrize并设置ids参数时,重复的ID字符串会导致错误,因为参数化测试中的测试ID必须唯一。
English
When using pytest.mark.parametrize with ids parameter, duplicate ID strings cause an error because test IDs must be unique within a parametrized test.
解决方案
-
95% 成功率 Ensure all IDs in the ids list are unique
@pytest.mark.parametrize('x', [1, 2, 3], ids=['case_1', 'case_2', 'case_3']) def test_example(x): ... Each ID must be different. -
90% 成功率 Use a function to generate unique IDs based on the parameter values
def id_func(val): return f'x={val}' @pytest.mark.parametrize('x', [1, 2, 3], ids=id_func) def test_example(x): ... This ensures uniqueness.
无效尝试
常见但无效的做法:
-
Removing the ids parameter entirely
40% 失败
This loses descriptive test names, making test output harder to read.
-
Using a lambda function that returns a constant string
70% 失败
This will still produce duplicate IDs if the lambda ignores input.