python
type_error
ai_generated
true
TypeError: ids must be a list of strings or callables, got <class 'int'>
ID: python/pytest-parametrize-ids-type-error
80%Fix Rate
90%Confidence
0Evidence
2024-05-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7.4.0 | active | — | — | — |
Root Cause
在 pytest.mark.parametrize 中提供了非字符串类型的 ids 参数,pytest 无法生成测试 ID。
generic中文
在 pytest.mark.parametrize 中提供了非字符串类型的 ids 参数,pytest 无法生成测试 ID。
Workarounds
-
100% success
在 parametrize 中提供字符串列表:`@pytest.mark.parametrize('arg', [1,2], ids=['one', 'two'])` -
95% success
`@pytest.mark.parametrize('arg', [1,2], ids=lambda x: f'arg_{x}')`
Dead Ends
Common approaches that don't work:
-
40% fail
如果 ids 是整数列表,强制转换可能丢失有意义的信息,且 pytest 期望字符串或可调用。
-
30% fail
移除 ids 后测试仍会运行,但失去可读性,且可能影响测试报告。