python
type_error
ai_generated
true
TypeError: test_func() 缺少 1 个必需的位置参数: 'value'
TypeError: test_func() missing 1 required positional argument: 'value'
ID: python/pytest-fixture-indirect-param-missing
80%修复率
88%置信度
0证据数
2024-09-21首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
使用了 pytest.mark.parametrize(..., indirect=True),但指定的 fixture 没有接受 'request' 参数来读取 request.param,因此 fixture 不返回内容,测试看到缺少参数。
English
pytest.mark.parametrize(..., indirect=True) was used, but the named fixture does not actually accept a 'request' argument to read request.param, so the fixture returns nothing and the test sees a missing argument.
解决方案
-
95% 成功率
@pytest.fixture def value(request): return request.param * 2 @pytest.mark.parametrize('value', [1, 2, 3], indirect=True) def test_func(value): assert value in (2, 4, 6) -
90% 成功率
@pytest.fixture def make_value(): def _make(n): return n * 2 return _make @pytest.mark.parametrize('n', [1, 2, 3]) def test_func(make_value, n): assert make_value(n) in (2, 4, 6)
无效尝试
常见但无效的做法:
-
75% 失败
Changes the fixture into a direct parameter, so the fixture logic (setup, teardown) never runs.
-
80% 失败
Hides the real problem; the fixture still does not receive the parametrized value.