0
0
PyTesttesting~20 mins

Factory fixtures in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factory Fixture Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.
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']}")
AValue is '10'
BAssertionError
CValue is 10
DTypeError
Attempts:
2 left
💡 Hint
Look at how the parameter is passed and used inside the fixture.
assertion
intermediate
1: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?
Aassert len(user_factory) == 3
Bassert user_factory.count == 3
Cassert user_factory.size() == 3
Dassert user_factory.length == 3
Attempts:
2 left
💡 Hint
Remember how to get the length of a list in Python.
🔧 Debug
advanced
2: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
ANo error, tests pass
BTypeError
CValueError
DFixtureLookupError
Attempts:
2 left
💡 Hint
Check how the fixture is used with and without parameters.
🧠 Conceptual
advanced
1:30remaining
Purpose of factory fixtures in pytest
What is the main advantage of using factory fixtures in pytest?
ATo automatically generate test reports in different formats
BTo create reusable test data or objects dynamically for multiple tests
CTo replace the need for test parametrization entirely
DTo speed up test execution by caching results automatically
Attempts:
2 left
💡 Hint
Think about why you might want to generate data or objects fresh for each test.
framework
expert
2: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?
AUse function scope and yield the resource, then cleanup after yield
BUse session scope and cleanup in a finalizer registered with addfinalizer
CUse module scope and cleanup manually inside the test function
DUse class scope and rely on garbage collection for cleanup
Attempts:
2 left
💡 Hint
Consider how long the resource is needed and how pytest manages fixture cleanup.