Bird
Raised Fist0
PyTesttesting~10 mins

Dynamic fixture selection in PyTest - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a pytest fixture named 'data' that returns a list.

PyTest
import pytest

@pytest.fixture
def data():
    return [1]
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
B(1, 2, 3)
C{1, 2, 3}
D'123'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple or set instead of a list.
Returning a string instead of a list.
2fill in blank
medium

Complete the code to select a fixture dynamically based on the 'request' parameter.

PyTest
import pytest

@pytest.fixture
def dynamic_data(request):
    fixture_name = 'data_' + request.param
    return request.getfixturevalue([1])
Drag options to blanks, or click blank then click option'
A'fixture_name'
Bfixture_name
Crequest.param
D'data_'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the string 'fixture_name' instead of the variable.
Passing request.param directly instead of the full fixture name.
3fill in blank
hard

Fix the error in the test function to use the dynamic fixture with parameterization.

PyTest
@pytest.mark.parametrize('dynamic_data', ['one', 'two'], indirect=True)
def test_dynamic(dynamic_data):
    assert isinstance(dynamic_data, list) and len(dynamic_data) == [1]
Drag options to blanks, or click blank then click option'
A1
B2
C3
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Assuming the length is 2 or 1.
Using zero which would always fail.
4fill in blank
hard

Fill both blanks to define two fixtures 'data_one' and 'data_two' returning different lists.

PyTest
@pytest.fixture
def data_one():
    return [1]

@pytest.fixture
def data_two():
    return [2]
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
B[4, 5]
C[7, 8, 9]
D[10]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same list for both fixtures.
Choosing lists with different lengths causing test failures.
5fill in blank
hard

Fill all three blanks to create a test that uses dynamic fixture selection and asserts the first element.

PyTest
@pytest.mark.parametrize('dynamic_data', ['one', 'two'], indirect=True)
def test_first_element(dynamic_data):
    assert dynamic_data[[1]] == [2] and len(dynamic_data) == [3]
Drag options to blanks, or click blank then click option'
A0
B1
C3
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 for the first element.
Incorrect length value causing assertion failure.

Practice

(1/5)
1. What is the main purpose of dynamic fixture selection in pytest?
easy
A. To automatically generate test data without fixtures
B. To statically assign fixtures before running tests
C. To choose which fixture to use during test execution based on runtime conditions
D. To skip tests that use fixtures

Solution

  1. Step 1: Understand fixture selection

    Dynamic fixture selection means deciding which fixture to use while the test runs, not before.
  2. Step 2: Compare options

    Only To choose which fixture to use during test execution based on runtime conditions describes choosing fixtures during test execution based on conditions.
  3. Final Answer:

    To choose which fixture to use during test execution based on runtime conditions -> Option C
  4. Quick Check:

    Dynamic fixture selection = runtime choice [OK]
Hint: Dynamic means choosing fixtures while tests run [OK]
Common Mistakes:
  • Confusing static fixture assignment with dynamic selection
  • Thinking fixtures generate test data automatically
  • Believing dynamic selection skips tests
2. Which of the following is the correct syntax to select a fixture dynamically inside a test using request.getfixturevalue()?
easy
A. fixture_name = request.getfixturevalue('my_fixture')
B. fixture_name = request.getfixture('my_fixture')
C. fixture_name = request.getfixturevalue(my_fixture)
D. fixture_name = request.getfixture('my_fixture')()

Solution

  1. Step 1: Recall correct method name

    The method to get a fixture by name dynamically is getfixturevalue, not getfixture.
  2. Step 2: Check argument type

    The fixture name must be a string, so it should be quoted.
  3. Final Answer:

    fixture_name = request.getfixturevalue('my_fixture') -> Option A
  4. Quick Check:

    Use getfixturevalue('fixture_name') [OK]
Hint: Use getfixturevalue with string fixture name [OK]
Common Mistakes:
  • Using getfixture instead of getfixturevalue
  • Passing fixture name without quotes
  • Calling getfixture as a function
3. Given the code below, what will be the output when running test_dynamic_fixture?
import pytest

@pytest.fixture
def fixture_a():
    return 'A'

@pytest.fixture
def fixture_b():
    return 'B'

@pytest.mark.parametrize('use_a', [True, False])
def test_dynamic_fixture(request, use_a):
    fixture_name = 'fixture_a' if use_a else 'fixture_b'
    value = request.getfixturevalue(fixture_name)
    print(value)
    assert value in ['A', 'B']
medium
A. Prints 'A' twice and passes both tests
B. Prints 'B' then 'A' and fails one test
C. Raises an error because getfixturevalue is used incorrectly
D. Prints 'A' then 'B' and passes both tests

Solution

  1. Step 1: Understand parameterization

    The test runs twice: once with use_a=True, once with use_a=False.
  2. Step 2: Trace fixture selection and output

    When use_a=True, fixture_name='fixture_a' returns 'A'. When False, fixture_name='fixture_b' returns 'B'. Both print and assert succeed.
  3. Final Answer:

    Prints 'A' then 'B' and passes both tests -> Option D
  4. Quick Check:

    Param True->A, False->B [OK]
Hint: Param True uses fixture_a, False uses fixture_b [OK]
Common Mistakes:
  • Assuming getfixturevalue causes error
  • Mixing order of printed values
  • Thinking test fails due to assertion
4. Identify the error in the following test code that tries to select a fixture dynamically:
import pytest

@pytest.fixture
def fix1():
    return 10

@pytest.fixture
def fix2():
    return 20

@pytest.mark.parametrize('name', ['fix1', 'fix2'])
def test_select(request, name):
    value = request.getfixture(name)
    assert value in [10, 20]
medium
A. Incorrect assert statement
B. Using request.getfixture instead of request.getfixturevalue
C. Missing parentheses when calling fixtures
D. Passing fixture name as a string instead of variable

Solution

  1. Step 1: Check method name for dynamic fixture

    The correct method to get a fixture dynamically is getfixturevalue, not getfixture.
  2. Step 2: Confirm usage of fixture name

    The fixture name is correctly passed as a string variable, so no issue there.
  3. Final Answer:

    Using request.getfixture instead of request.getfixturevalue -> Option B
  4. Quick Check:

    Use getfixturevalue, not getfixture [OK]
Hint: Remember method name: getfixturevalue, not getfixture [OK]
Common Mistakes:
  • Confusing method names for fixture retrieval
  • Passing fixture name incorrectly
  • Misunderstanding assert logic
5. You want to write a test that uses different database connection fixtures based on an environment variable DB_TYPE. Which approach correctly implements dynamic fixture selection in pytest? A) Use request.getfixturevalue() inside the test to select the fixture by name. B) Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones. C) Use pytest_generate_tests() hook to parametrize fixtures. D) Use yield in fixtures to switch dynamically.
hard
A. Use request.getfixturevalue() inside the test to select the fixture by name.
B. Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones.
C. Use pytest_generate_tests() hook to parametrize fixtures.
D. Use yield in fixtures to switch dynamically.

Solution

  1. Step 1: Understand environment-based fixture selection

    Choosing a fixture based on an environment variable requires runtime decision inside the test.
  2. Step 2: Evaluate options for dynamic selection

    Use request.getfixturevalue() inside the test to select the fixture by name. uses request.getfixturevalue() to pick the fixture by name at runtime, which fits the need.
  3. Step 3: Exclude other options

    Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones. applies all fixtures upfront, not dynamic. Use pytest_generate_tests() hook to parametrize fixtures. is for test parametrization, not fixture selection. Use yield in fixtures to switch dynamically. misuses yield; it does not switch fixtures dynamically.
  4. Final Answer:

    Use request.getfixturevalue() inside the test to select the fixture by name. -> Option A
  5. Quick Check:

    Dynamic fixture selection = getfixturevalue() [OK]
Hint: Use getfixturevalue() for runtime fixture choice [OK]
Common Mistakes:
  • Trying to parametrize fixtures instead of selecting dynamically
  • Using usefixtures decorator incorrectly
  • Misunderstanding yield usage in fixtures
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard