0
0
PyTesttesting~20 mins

Fixture as function argument in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest fixture usage in test function
What is the output when running this pytest test function that uses a fixture as an argument?
PyTest
import pytest

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

def test_sum(sample_data):
    assert sum(sample_data) == 6
    print('Sum is:', sum(sample_data))
ASum is: 6\nPASSED
BSum is: 6\nFAILED
CTypeError: test_sum() missing 1 required positional argument: 'sample_data'
DNameError: name 'sample_data' is not defined
Attempts:
2 left
💡 Hint
Remember that pytest automatically injects fixtures by matching argument names.
assertion
intermediate
2:00remaining
Correct assertion using fixture data
Given a fixture that returns a dictionary, which assertion correctly verifies the value of key 'status' in the test function?
PyTest
import pytest

@pytest.fixture
def response():
    return {'status': 'ok', 'code': 200}

def test_response_status(response):
    # Which assertion is correct here?
Aassert response['status'] != 'ok'
Bassert response.status == 'ok'
Cassert response.get('status') = 'ok'
Dassert response['status'] == 'ok'
Attempts:
2 left
💡 Hint
Dictionaries use square brackets to access keys.
🔧 Debug
advanced
2:00remaining
Identify the error when fixture is not passed as argument
What error occurs when a test function tries to use a fixture variable without declaring it as a function argument?
PyTest
import pytest

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

def test_data_sum():
    assert sum(data) == 60
AAssertionError: assert 60 == 60
BNameError: name 'data' is not defined
CTypeError: test_data_sum() missing 1 required positional argument: 'data'
DNo error, test passes
Attempts:
2 left
💡 Hint
Fixtures must be declared as parameters to be injected.
framework
advanced
2:00remaining
How pytest discovers and injects fixtures
Which statement best describes how pytest uses fixtures as function arguments in tests?
APytest matches test function argument names to fixture names and injects the fixture return value automatically.
BPytest requires manual import of fixtures inside each test function before use.
CFixtures must be called inside the test function to get their values.
DPytest only injects fixtures if they are decorated with @pytest.inject.
Attempts:
2 left
💡 Hint
Think about how pytest knows which fixture to use for each argument.
locator
expert
2:00remaining
Selecting the correct fixture usage pattern in pytest
Which of the following test function signatures correctly uses a fixture named 'db_connection' to ensure the fixture is injected?
A
def test_query():
    db_connection = db_connection()
B
def test_query():
    from fixtures import db_connection
    db_connection()
Cdef test_query(db_connection):
Ddef test_query(db_connection=None):
Attempts:
2 left
💡 Hint
Fixtures are injected by naming them as parameters without default values.