Challenge - 5 Problems
Factory Fixture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a pytest factory fixture with parameter
What is the output of this pytest test when run?
Consider the factory fixture creates a dictionary with a key 'value' set to the parameter.
Consider the factory fixture creates a dictionary with a key 'value' set to the parameter.
PyTest
import pytest @pytest.fixture @pytest.mark.parametrize('data_factory', [10], indirect=True) def data_factory(request): return {"value": request.param} def test_value(data_factory): assert data_factory["value"] == 10 print(f"Value is {data_factory['value']}")
Attempts:
2 left
💡 Hint
Look at how the parameter is passed and used inside the fixture.
✗ Incorrect
The fixture receives the parameter 10 as an integer and returns a dictionary with key 'value' set to 10. The assertion passes and the print outputs 'Value is 10'.
❓ assertion
intermediate1:30remaining
Correct assertion for a factory fixture output
Given a factory fixture that returns a list of user names, which assertion correctly verifies the list contains exactly 3 users?
PyTest
import pytest @pytest.fixture def user_factory(): return ["alice", "bob", "carol"] def test_users(user_factory): # Which assertion is correct here?
Attempts:
2 left
💡 Hint
Remember how to get the length of a list in Python.
✗ Incorrect
The correct way to check the number of items in a list is using len(list). The other options use invalid attributes or methods.
🔧 Debug
advanced2:00remaining
Identify the error in this factory fixture usage
What error will this pytest code raise when running the test?
PyTest
import pytest @pytest.fixture def number_factory(): return 5 def test_number(number_factory): assert number_factory == 5 @pytest.mark.parametrize('number_factory', [10], indirect=True) def test_number_param(number_factory): assert number_factory == 10
Attempts:
2 left
💡 Hint
Check how the fixture is used with and without parameters.
✗ Incorrect
The fixture 'number_factory' is not designed to accept parameters, but the second test tries to parametrize it indirectly, causing pytest to raise FixtureLookupError.
🧠 Conceptual
advanced1:30remaining
Purpose of factory fixtures in pytest
What is the main advantage of using factory fixtures in pytest?
Attempts:
2 left
💡 Hint
Think about why you might want to generate data or objects fresh for each test.
✗ Incorrect
Factory fixtures help create fresh, reusable test data or objects dynamically, improving test isolation and reducing duplication.
❓ framework
expert2:30remaining
Best practice for factory fixture scope and cleanup
Which pytest fixture scope and cleanup method is best for a factory fixture that creates temporary files needed only during each test?
Attempts:
2 left
💡 Hint
Consider how long the resource is needed and how pytest manages fixture cleanup.
✗ Incorrect
Function scope ensures the fixture runs fresh for each test. Using yield allows cleanup code after the test finishes, ensuring temporary files are removed properly.