Test fixture dependency using pytest fixtures
Preconditions (2)
✅ Expected Result: The test passes successfully, confirming that the fixture 'user_data' correctly uses the 'db_connection' fixture and provides expected data
import pytest @pytest.fixture def db_connection(): # Simulate database connection setup return 'db_connected' @pytest.fixture def user_data(db_connection): # Use db_connection fixture to prepare user data return {'user': 'test_user', 'status': 'active'} def test_user_status(user_data): assert user_data['status'] == 'active'
The db_connection fixture simulates setting up a database connection and returns a string.
The user_data fixture depends on db_connection by accepting it as a parameter. This shows fixture dependency.
The test function test_user_status uses the user_data fixture and asserts that the user status is 'active'.
This structure keeps setup code reusable and clean, demonstrating how one fixture can use another.
Now add a third fixture 'admin_user_data' that depends on 'db_connection' and returns {'user': 'admin', 'status': 'active', 'role': 'admin'}. Write a test to verify the 'role' is 'admin'.
import pytest
@pytest.fixture
def base():
return 10
@pytest.fixture
def add_five(base):
return base + 5
def test_sum(add_five):
assert add_five == 15
print(add_five)import pytest
@pytest.fixture
def data():
return [1, 2, 3]
@pytest.fixture
def processed_data():
data.append(4)
return datafull_config that depends on two other fixtures: base_config returning a dict {'host': 'localhost'} and auth_config returning {'user': 'admin'}. Which code correctly combines these fixtures so full_config returns {'host': 'localhost', 'user': 'admin'}?Pass the test and this pattern moves unknown