python type_error ai_generated true

TypeError: ids must be a list of strings or None, got int

ID: python/pytest-parametrize-id-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2024-07-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.8 active
3.9 active

Root Cause

The 'ids' parameter in @pytest.mark.parametrize is provided as a non-string iterable (e.g., list of integers) instead of strings.

generic

中文

@pytest.mark.parametrize 中的 'ids' 参数被提供为非字符串可迭代对象(例如整数列表),而不是字符串。

Workarounds

  1. 95% success Convert ids to a list of strings using str() or a lambda.
    @pytest.mark.parametrize('arg', [1, 2], ids=[str(x) for x in [1, 2]])
  2. 90% success Use a lambda function to generate ids dynamically.
    @pytest.mark.parametrize('arg', [1, 2], ids=lambda x: f'case_{x}')

Dead Ends

Common approaches that don't work:

  1. Using list comprehension to convert ids to strings but forgetting to pass them 60% fail

    If the conversion is not applied to the ids parameter, the error persists.

  2. Omitting the ids parameter entirely 50% fail

    Tests will run without custom IDs, but the error is avoided; however, test reports may be less readable.