python data_error ai_generated true

ValueError: duplicate 'test_id' in parametrize

ID: python/pytest-parametrize-id-conflict

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-04-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active
8.x active

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 75% fail

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

  2. 90% fail

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