0
0
PyTesttesting~10 mins

Fixture as function argument 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 use the fixture as a function argument.

PyTest
import pytest

@pytest.fixture
def sample_data():
    return [1, 2, 3]

def test_sum([1]):
    assert sum(sample_data) == 6
Drag options to blanks, or click blank then click option'
Afixture
Bsample_data
Cdata
Dinput_data
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different argument name than the fixture name.
Not passing the fixture as an argument at all.
2fill in blank
medium

Complete the test function argument to receive the fixture value.

PyTest
import pytest

@pytest.fixture
def user_info():
    return {'name': 'Alice', 'age': 30}

def test_user_age([1]):
    assert user_info['age'] == 30
Drag options to blanks, or click blank then click option'
Ainfo
Buser
Cuser_info
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different argument name than the fixture name.
Trying to access the fixture without passing it as an argument.
3fill in blank
hard

Fix the error in the test function argument to correctly use the fixture.

PyTest
import pytest

@pytest.fixture
def numbers():
    return [10, 20, 30]

def test_max([1]):
    assert max(numbers) == 30
Drag options to blanks, or click blank then click option'
Anumbers
Bdata
Cnumber_list
Dnums
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different argument name than the fixture name.
Not passing the fixture as an argument.
4fill in blank
hard

Fill both blanks to correctly use two fixtures as function arguments.

PyTest
import pytest

@pytest.fixture
def first_name():
    return 'John'

@pytest.fixture
def last_name():
    return 'Doe'

def test_full_name([1], [2]):
    full_name = first_name + ' ' + last_name
    assert full_name == 'John Doe'
Drag options to blanks, or click blank then click option'
Afirst_name
Bfull_name
Clast_name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect argument names.
Passing only one fixture instead of both.
5fill in blank
hard

Fill all three blanks to correctly use fixtures and assert the combined result.

PyTest
import pytest

@pytest.fixture
def city():
    return 'Paris'

@pytest.fixture
def country():
    return 'France'

def test_location([1], [2]):
    location = city + ', ' + country
    assert location == 'Paris, France'
Drag options to blanks, or click blank then click option'
Acity
Bcountry
Clocation
Dloc
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing both fixtures as arguments.
Using wrong variable names in the assertion.