Bird
0
0

Identify the issue in the following pytest code snippet:

medium📝 Debug Q6 of 15
PyTest - Parametrize
Identify the issue in the following pytest code snippet:
import pytest

@pytest.fixture
def settings(request):
    return request.param * 10

@pytest.mark.parametrize('settings', [2, 3], indirect=True)
def test_settings(settings):
    assert settings > 0

@pytest.mark.parametrize('settings', [4, 5])
def test_settings_direct(settings):
    assert settings > 0
AThe second test <code>test_settings_direct</code> will fail because it lacks <code>indirect=True</code> for the fixture.
BThe fixture should not use <code>request.param</code> when indirect=True is set.
CBoth tests will pass without issues.
DThe fixture <code>settings</code> cannot be used with direct parametrization without indirect=True.
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture usage

    The fixture expects parameters via request.param, which only works if indirect=True is set.
  2. Step 2: Analyze second test

    The second test uses direct parametrization without indirect=True, so settings is passed directly as an argument, not processed by the fixture.
  3. Step 3: Result

    This causes a mismatch because the fixture is not invoked, and the test parameter is not transformed.
  4. Final Answer:

    The fixture settings cannot be used with direct parametrization without indirect=True. -> Option D
  5. Quick Check:

    Fixture needs indirect=True to receive params [OK]
Quick Trick: Fixtures with request.param require indirect=True [OK]
Common Mistakes:
MISTAKES
  • Using fixture parameters without indirect=True
  • Assuming direct param passes through fixture automatically
  • Ignoring fixture parameter access via request.param

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes