python
config_error
ai_generated
true
在 test_math.py 中:@pytest.mark.parametrize 参数名 ['a', 'b'] 与测试函数签名 ['x', 'y'] 不匹配
In test_math.py: @pytest.mark.parametrize argument names ['a', 'b'] do not match the test function signature ['x', 'y']
ID: python/pytest-parametrize-marks-mismatch
80%修复率
90%置信度
0证据数
2024-08-09首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
parametrize 的 argnames 字符串与测试函数的参数名不匹配。pytest 要求名称完全一致(对元组参数而言顺序也重要)。
English
The parametrize argnames string does not match the test function's parameter names. pytest requires exact names (order matters for tuple args).
解决方案
-
98% 成功率
Match names exactly: @pytest.mark.parametrize('x,y', [(1,2),(3,4)]) def test_add(x, y): ... Or use a list of names: @pytest.mark.parametrize(['x','y'], [...]) -
95% 成功率
Use pytest.param with ids for clarity: @pytest.mark.parametrize('x,y', [pytest.param(1,2,id='small')])
无效尝试
常见但无效的做法:
-
90% 失败
pytest inspects the signature and rejects **kwargs for parametrization; test still errors.
-
80% 失败
pytest treats 'args' as a single parameter, breaking tuple unpacking.