python type_error ai_generated true

类型错误:ids 必须是字符串列表或 None,但收到了 int

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

ID: python/pytest-parametrize-id-error

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

版本兼容性

版本状态引入弃用备注
3.8 active
3.9 active

根因分析

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

English

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

generic

解决方案

  1. 95% 成功率 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% 成功率 Use a lambda function to generate ids dynamically.
    @pytest.mark.parametrize('arg', [1, 2], ids=lambda x: f'case_{x}')

无效尝试

常见但无效的做法:

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

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

  2. Omitting the ids parameter entirely 50% 失败

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