Challenge - 5 Problems
Fixture Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple fixture factory
What is the output of this pytest test when run?
Note: The fixture factory creates a user object with a given name.
Note: The fixture factory creates a user object with a given name.
PyTest
import pytest @pytest.fixture def user_factory(): def create_user(name): return {"name": name, "active": True} return create_user def test_user_active(user_factory): user = user_factory("Alice") assert user["active"] print(user["name"])
Attempts:
2 left
💡 Hint
Focus on what the fixture factory returns and what the test prints.
✗ Incorrect
The fixture factory returns a function that creates a user dict with the given name and active status True. The test asserts active is True (passes) and prints the name, which is 'Alice'.
❓ assertion
intermediate2:00remaining
Correct assertion for fixture factory output
Given this fixture factory, which assertion correctly verifies the created user's age is 30?
PyTest
import pytest @pytest.fixture def user_factory(): def create_user(name, age): return {"name": name, "age": age} return create_user def test_user_age(user_factory): user = user_factory("Bob", 30) # Which assertion is correct here?
Attempts:
2 left
💡 Hint
Remember how dictionaries are accessed in Python.
✗ Incorrect
The user is a dictionary, so to check age, use user["age"] == 30. Option A fails because dicts don't have attribute access. Option A checks name, not age. Option A asserts the opposite.
🔧 Debug
advanced2:00remaining
Identify the error in fixture factory usage
This test fails with a TypeError. What is the cause?
Code:
import pytest @pytest.fixture def user_factory(): def create_user(name, age=25): return {"name": name, "age": age} return create_user def test_user(user_factory): user = user_factory() assert user["age"] == 25
Code:
import pytest @pytest.fixture def user_factory(): def create_user(name, age=25): return {"name": name, "age": age} return create_user def test_user(user_factory): user = user_factory() assert user["age"] == 25
Attempts:
2 left
💡 Hint
Check the parameters of the inner function and how it is called.
✗ Incorrect
The inner function create_user requires 'name' as a positional argument without default. Calling user_factory() without arguments causes TypeError.
❓ framework
advanced2:00remaining
Best practice for fixture factory scope
Which scope is most appropriate for a fixture factory that creates fresh test data for each test?
Attempts:
2 left
💡 Hint
Think about test isolation and data freshness.
✗ Incorrect
Function scope runs the fixture for each test function, ensuring fresh data. Other scopes reuse data, risking shared state.
🧠 Conceptual
expert2:00remaining
Why use fixture factories instead of simple fixtures?
Which reason best explains why fixture factories are preferred over simple fixtures when creating test objects with varying attributes?
Attempts:
2 left
💡 Hint
Consider flexibility in test data creation.
✗ Incorrect
Fixture factories return functions that accept parameters, enabling tests to create customized objects. Simple fixtures return fixed data.