python config_error ai_generated true

ValueError: duplicate test IDs: 'test_case_0'

ID: python/pytest-parametrize-ids-duplicate

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-08-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.0.0 active
8.0.0 active

Root Cause

When using pytest.mark.parametrize with ids parameter, duplicate ID strings cause an error because test IDs must be unique within a parametrized test.

generic

中文

当使用pytest.mark.parametrize并设置ids参数时,重复的ID字符串会导致错误,因为参数化测试中的测试ID必须唯一。

Workarounds

  1. 95% success 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.
  2. 90% success 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.

Dead Ends

Common approaches that don't work:

  1. Removing the ids parameter entirely 40% fail

    This loses descriptive test names, making test output harder to read.

  2. Using a lambda function that returns a constant string 70% fail

    This will still produce duplicate IDs if the lambda ignores input.