python
data_error
ai_generated
true
ValueError: parametrize 中的 'test_id' 重复
ValueError: duplicate 'test_id' in parametrize
ID: python/pytest-parametrize-id-conflict
80%修复率
88%置信度
0证据数
2024-04-25首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
两个或多个参数化测试用例具有相同的 ID,可能是通过 ids 参数显式设置,或从相同的参数值自动生成。
English
Two or more parametrized test cases have the same ID, either explicitly set via the ids argument or generated automatically from identical parameter values.
解决方案
-
95% 成功率
@pytest.mark.parametrize('x,y', [(1,2), (1,2)], ids=['case1', 'case2']) def test_add(x, y): assert x + y == 3 -
90% 成功率
@pytest.mark.parametrize('x,y,case', [(1,2,'a'), (1,2,'b')]) def test_add(x, y, case): assert x + y == 3
无效尝试
常见但无效的做法:
-
75% 失败
pytest will still generate duplicate IDs if the parameter values are identical, causing the same error.
-
90% 失败
The conflict is in the parametrize IDs, not the test function name. Renaming the function does not resolve the duplicate ID.