python config_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-03-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
6.x active — — —
7.x active — — —
8.x active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

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

  2. 98% fail

    Fixture names cannot contain dots; pytest rejects the definition.