0
0
PyTesttesting~10 mins

Dynamic fixture selection in PyTest - Interactive Code Practice

Choose your learning style9 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.