python config_error ai_generated true

ValueError: 在 parametrize 中重复的 'test_id'

ValueError: duplicate 'test_id' in parametrize

ID: python/pytest-parametrize-id-duplicate

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-06-01首次发现

版本兼容性

版本状态引入弃用备注
7.x active
8.x active

根因分析

两个或多个参数化用例具有相同的 ID,要么通过 ids 显式设置,要么从相同的参数值自动生成。

English

Two or more parametrized cases have the same ID, either explicitly set via ids or generated automatically from identical parameter values.

generic

解决方案

  1. 95% 成功率
    @pytest.mark.parametrize('x', [1, 1], ids=['first', 'second'])
    def test_foo(x): ...
  2. 90% 成功率
    def idfn(val):
        return f'val-{val}-{uuid.uuid4()}'
    
    @pytest.mark.parametrize('x', [1, 1], ids=idfn)

无效尝试

常见但无效的做法:

  1. 70% 失败

    If the parameter values are identical, pytest still generates duplicate IDs.

  2. 90% 失败

    The duplication is within the same test's parametrization, not across tests.