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

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 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)
  2. 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)

无效尝试

常见但无效的做法:

  1. 75% 失败

    Changes the fixture into a direct parameter, so the fixture logic (setup, teardown) never runs.

  2. 80% 失败

    Hides the real problem; the fixture still does not receive the parametrized value.