0
0
PyTesttesting~20 mins

Fixture composition in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of composed pytest fixtures
What is the output when running this pytest test with the given fixtures?
PyTest
import pytest

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

@pytest.fixture
def extended_data(base_data):
    return base_data + [4, 5]

def test_sum(extended_data):
    assert sum(extended_data) == 15
    print(sum(extended_data))
ATypeError
B15
C14
DAssertionError
Attempts:
2 left
💡 Hint
Think about how fixtures can use other fixtures as input and how the list is extended.
assertion
intermediate
2:00remaining
Correct assertion for nested fixture output
Given these fixtures, which assertion correctly verifies the final output in the test?
PyTest
import pytest

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

@pytest.fixture
def user_with_email(user):
    user['email'] = 'alice@example.com'
    return user

def test_user_email(user_with_email):
    # Which assertion is correct here?
Aassert user_with_email['email'] == 'bob@example.com'
Bassert user_with_email['age'] == 25
Cassert 'email' not in user_with_email
Dassert user_with_email['email'] == 'alice@example.com' and user_with_email['age'] == 30
Attempts:
2 left
💡 Hint
Check how the email is added in the second fixture and the age remains unchanged.
🔧 Debug
advanced
2:00remaining
Identify the cause of fixture reuse error
Why does this pytest test raise an error about fixture scope when running?
PyTest
import pytest

@pytest.fixture(scope='function')
def db_connection():
    return 'db_conn'

@pytest.fixture(scope='module')
def user_session(db_connection):
    return f'session using {db_connection}'

def test_session(user_session):
    assert 'session' in user_session
AFixture 'user_session' has a wider scope than 'db_connection', causing a scope mismatch error.
BThe 'db_connection' fixture is missing a return statement.
CThe test function is missing a parameter for 'db_connection'.
DThe 'user_session' fixture should not depend on another fixture.
Attempts:
2 left
💡 Hint
Think about pytest fixture scopes and their allowed dependencies.
framework
advanced
2:00remaining
Best practice for fixture composition in pytest
Which option correctly describes a best practice when composing fixtures in pytest?
AFixtures should never return mutable objects to avoid side effects.
BAvoid using fixtures as parameters to other fixtures; use global variables instead.
CAlways keep fixture scopes compatible; a fixture should not depend on another fixture with a narrower scope.
DUse fixtures only for setup, never for providing test data.
Attempts:
2 left
💡 Hint
Think about how pytest manages fixture lifetimes and dependencies.
🧠 Conceptual
expert
3:00remaining
Understanding fixture teardown order in composed fixtures
Given these fixtures, in what order will the teardown code run after the test completes?
PyTest
import pytest

@pytest.fixture
def resource_a():
    print('Setup A')
    yield 'A'
    print('Teardown A')

@pytest.fixture
def resource_b(resource_a):
    print('Setup B')
    yield 'B'
    print('Teardown B')

def test_resources(resource_b):
    print('Test running')
A1,2,3,4,5
B2,1,3,4,5
C1,2,3,5,4
D2,1,3,5,4
Attempts:
2 left
💡 Hint
Fixtures teardown in reverse order of setup, respecting dependencies.