python config_error ai_generated true

fixture 'request.param' 未找到 可用 fixtures: ...

fixture 'request.param' not found available fixtures: ...

ID: python/pytest-parametrize-indirect-missing-fixture

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-03-19首次发现

版本兼容性

版本状态引入弃用备注
6.x active — — —
7.x active — — —
8.x active — — —

根因分析

在 @pytest.mark.parametrize 中使用 indirect=True 要求参数名匹配已存在的 fixture。如果 fixture 缺失或命名错误,pytest 会尝试将 'request.param' 解析为 fixture 并失败。

English

indirect=True in @pytest.mark.parametrize requires the parameter name to match an existing fixture. If the fixture is missing or misnamed, pytest tries to resolve 'request.param' as a fixture and fails.

generic

解决方案

  1. 95% 成功率
    @pytest.fixture
    def user(request):
        return User(name=request.param)
    
    @pytest.mark.parametrize('user', ['alice', 'bob'], indirect=True)
    def test_user(user):
        ...
  2. 90% 成功率
    @pytest.mark.parametrize('x,y', [(1,2),(3,4)], indirect=['x'])
    def test_xy(x, y):  # x goes through fixture, y is raw
        ...

无效尝试

常见但无效的做法:

  1. 70% 失败

    Removes the fixture indirection entirely; the test may now receive raw params instead of fixture-processed values, breaking test logic.

  2. 98% 失败

    Fixture names cannot contain dots; pytest rejects the definition.