0
0
PyTesttesting~20 mins

Fixture factories in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Factory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.
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"])
AKeyError
BTrue
CAlice
DAssertionError
Attempts:
2 left
💡 Hint
Focus on what the fixture factory returns and what the test prints.
assertion
intermediate
2: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?
Aassert user["age"] == 30
Bassert user.age == 30
Cassert user.get("name") == 30
Dassert user["age"] != 30
Attempts:
2 left
💡 Hint
Remember how dictionaries are accessed in Python.
🔧 Debug
advanced
2: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
Auser_factory() is called without required positional argument 'name'
BThe fixture factory returns None instead of a function
CThe assertion compares wrong key 'age' instead of 'name'
DThe fixture is missing the @pytest.mark.usefixtures decorator
Attempts:
2 left
💡 Hint
Check the parameters of the inner function and how it is called.
framework
advanced
2:00remaining
Best practice for fixture factory scope
Which scope is most appropriate for a fixture factory that creates fresh test data for each test?
A"class" scope to share data across test methods in a class
B"module" scope to reuse data across tests in a module
C"session" scope to share data across all tests in session
D"function" scope to ensure fresh data per test
Attempts:
2 left
💡 Hint
Think about test isolation and data freshness.
🧠 Conceptual
expert
2: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?
AFixture factories reduce the number of test functions needed
BFixture factories allow dynamic creation of objects with different parameters per test
CSimple fixtures run faster and are better for varying test data
DSimple fixtures automatically reset database state between tests
Attempts:
2 left
💡 Hint
Consider flexibility in test data creation.