python data_error ai_generated true

ValueError: parametrize 中的 'test_id' 重复

ValueError: duplicate 'test_id' in parametrize

ID: python/pytest-parametrize-id-conflict

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 95% 成功率
    @pytest.mark.parametrize('x,y', [(1,2), (1,2)], ids=['case1', 'case2'])
    def test_add(x, y):
        assert x + y == 3
  2. 90% 成功率
    @pytest.mark.parametrize('x,y,case', [(1,2,'a'), (1,2,'b')])
    def test_add(x, y, case):
        assert x + y == 3

无效尝试

常见但无效的做法:

  1. 75% 失败

    pytest will still generate duplicate IDs if the parameter values are identical, causing the same error.

  2. 90% 失败

    The conflict is in the parametrize IDs, not the test function name. Renaming the function does not resolve the duplicate ID.