python
type_error
ai_generated
true
类型错误:ids 必须是字符串列表或可调用对象,得到 <class 'int'>
TypeError: ids must be a list of strings or callables, got <class 'int'>
ID: python/pytest-parametrize-ids-type-error
80%修复率
90%置信度
0证据数
2024-05-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.4.0 | active | — | — | — |
根因分析
在 pytest.mark.parametrize 中提供了非字符串类型的 ids 参数,pytest 无法生成测试 ID。
English
在 pytest.mark.parametrize 中提供了非字符串类型的 ids 参数,pytest 无法生成测试 ID。
解决方案
-
100% 成功率
在 parametrize 中提供字符串列表:`@pytest.mark.parametrize('arg', [1,2], ids=['one', 'two'])` -
95% 成功率
`@pytest.mark.parametrize('arg', [1,2], ids=lambda x: f'arg_{x}')`
无效尝试
常见但无效的做法:
-
40% 失败
如果 ids 是整数列表,强制转换可能丢失有意义的信息,且 pytest 期望字符串或可调用。
-
30% 失败
移除 ids 后测试仍会运行,但失去可读性,且可能影响测试报告。