Challenge - 5 Problems
Fixture Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Remember that pytest automatically injects fixtures by matching argument names.
✗ Incorrect
The fixture 'sample_data' is passed automatically to the test function by pytest. The sum is 6, so the assertion passes and the print statement outputs 'Sum is: 6'.
❓ assertion
intermediate2: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?
Attempts:
2 left
💡 Hint
Dictionaries use square brackets to access keys.
✗ Incorrect
Option D correctly asserts that the value for key 'status' is 'ok'. Option D is invalid because dictionaries don't have attribute access. Option D uses assignment '=' instead of comparison '=='. Option D asserts the opposite of the correct condition.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Fixtures must be declared as parameters to be injected.
✗ Incorrect
The fixture 'data' is not passed as an argument, so the test function has no variable named 'data'. This causes a NameError.
❓ framework
advanced2:00remaining
How pytest discovers and injects fixtures
Which statement best describes how pytest uses fixtures as function arguments in tests?
Attempts:
2 left
💡 Hint
Think about how pytest knows which fixture to use for each argument.
✗ Incorrect
Pytest automatically injects fixtures by matching argument names to fixture functions. No manual import or calling is needed inside the test.
❓ locator
expert2: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?
Attempts:
2 left
💡 Hint
Fixtures are injected by naming them as parameters without default values.
✗ Incorrect
Option C correctly declares the fixture as a parameter. Option C tries to call the fixture like a function inside the test, which is incorrect. Option C manually imports and calls the fixture, which pytest does automatically. Option C sets a default None, so pytest will not inject the fixture.