0
0
PyTesttesting~10 mins

Why fixtures provide reusable test setup in PyTest - Test Your Understanding

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

Complete the code to define a simple pytest fixture named sample_data.

PyTest
import pytest

@pytest.[1]
def sample_data():
    return [1, 2, 3]
Drag options to blanks, or click blank then click option'
Asetup
Btest
Cfixture
Dparametrize
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.test instead of @pytest.fixture
Forgetting the decorator entirely
2fill in blank
medium

Complete the test function to use the sample_data fixture as a parameter.

PyTest
def test_sum([1]):
    assert sum(sample_data) == 6
Drag options to blanks, or click blank then click option'
Asample_data
Bfixture
Cdata
Dinput_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture
Not including the fixture as a parameter
3fill in blank
hard

Fix the error in the fixture usage by completing the code to return the correct data.

PyTest
@pytest.fixture
def numbers():
    return [1]
Drag options to blanks, or click blank then click option'
A[1, 2, 3]
B1, 2, 3
C{1, 2, 3}
D(1, 2, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Returning multiple values separated by commas without brackets
Returning a set instead of a list
4fill in blank
hard

Fill both blanks to create a fixture that sets up a dictionary and a test that uses it.

PyTest
@pytest.fixture
def [1]():
    return {'name': 'Alice', 'age': 30}

def test_user_info([2]):
    assert user['name'] == 'Alice'
Drag options to blanks, or click blank then click option'
Auser
Bdata
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for fixture and test parameter
Not matching the fixture name in the test function
5fill in blank
hard

Fill all three blanks to create a fixture that sets up a list and a test that checks its length and content.

PyTest
@pytest.fixture
def [1]():
    return ['apple', 'banana', 'cherry']

def test_fruits([2]):
    assert len(fruits) == [3]
Drag options to blanks, or click blank then click option'
Afruits
C3
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for fixture and test parameter
Incorrect length assertion