Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a factory fixture in pytest?
A factory fixture is a pytest fixture that returns a function to create test data or objects dynamically during a test. It helps generate multiple instances with different data easily.
Click to reveal answer
beginner
How do you define a factory fixture in pytest?
You define a factory fixture by creating a fixture that returns a function. This inner function can accept parameters to customize the created object for each test.
Click to reveal answer
intermediate
Why use factory fixtures instead of regular fixtures?
Factory fixtures allow creating multiple customized objects in one test, avoiding duplication and making tests more flexible and readable.
Click to reveal answer
beginner
Example: What does this factory fixture do?
<pre>import pytest
@pytest.fixture
def user_factory():
def create_user(name, age):
return {'name': name, 'age': age}
return create_user</pre>
This fixture returns a function create_user that takes name and age as inputs and returns a dictionary representing a user. Tests can call user_factory() to get this function and create users with different data.
Click to reveal answer
beginner
How do you use a factory fixture inside a test?
You add the factory fixture as a test argument. Then call the returned function with needed parameters to create test objects dynamically.
Click to reveal answer
What does a factory fixture return in pytest?
AA function to create test data
BA fixed test object
CA test report
DA test case
✗ Incorrect
Factory fixtures return a function that can create test data or objects dynamically.
Why are factory fixtures useful?
AThey speed up test execution by caching results
BThey replace the need for assertions
CThey automatically generate test reports
DThey allow creating multiple customized objects in tests
✗ Incorrect
Factory fixtures help create many customized objects easily within tests.
How do you access a factory fixture in a test function?
ABy calling pytest.run()
BBy importing it directly
CBy adding it as a parameter to the test function
DBy using a global variable
✗ Incorrect
You use factory fixtures by adding them as parameters to your test functions.
What is the main difference between a regular fixture and a factory fixture?
AFactory fixtures return a function; regular fixtures return fixed data
BRegular fixtures run faster
CFactory fixtures cannot accept parameters
DRegular fixtures are only for setup and teardown
✗ Incorrect
Factory fixtures return a function to create data dynamically, while regular fixtures return fixed data.
Which of these is a good use case for a factory fixture?
ARunning tests in parallel
BCreating multiple user objects with different names and ages
CGenerating test coverage reports
DMocking external APIs
✗ Incorrect
Factory fixtures are great for creating multiple test objects with varying data.
Explain what a factory fixture is and how it helps in pytest testing.
Think about how you can create many test objects easily.
You got /4 concepts.
Describe how to write and use a factory fixture in a pytest test.
Focus on the steps from fixture definition to usage.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a factory fixture in pytest?
easy
A. To run tests in parallel automatically
B. To create reusable test data with flexible parameters
C. To generate test reports in HTML format
D. To mock external API calls during tests
Solution
Step 1: Understand what factory fixtures do
Factory fixtures return a function that can create test data with different parameters as needed.
Step 2: Compare with other options
Running tests in parallel, generating reports, or mocking APIs are different pytest features, not factory fixtures.
Final Answer:
To create reusable test data with flexible parameters -> Option B
Quick Check:
Factory fixture = reusable flexible test data creator [OK]
Hint: Factory fixtures build test data functions fast [OK]
Common Mistakes:
Confusing factory fixtures with mocking
Thinking factory fixtures run tests
Assuming factory fixtures generate reports
2. Which of the following is the correct way to define a simple factory fixture in pytest?
easy
A. @pytest.fixture
def user_factory(name):
return {'name': name}
D. @pytest.fixture
def user_factory():
return {'name': 'default'}
Solution
Step 1: Identify factory fixture structure
A factory fixture returns a function that accepts parameters to create test data dynamically.
Step 2: Check each option
@pytest.fixture
def user_factory():
def create_user(name):
return {'name': name}
return create_user defines a fixture returning a function that takes a name and returns a dict, which is correct. Options A, C, and D do not return a function, so they are not factory fixtures.
A. The fixture is missing @pytest.mark.parametrize decorator; add it.
B. The fixture should not return a function; fix by returning a dict directly.
C. The test should not use the fixture as a function; fix by removing parentheses.
D. The factory function is missing the 'price' argument; fix by passing price when calling.
Solution
Step 1: Check the factory function parameters
The factory function create_item expects two arguments: name and price.
Step 2: Analyze the test call
The test calls item_factory('Book') with only one argument, missing price, causing an error or wrong data.
Final Answer:
The factory function is missing the 'price' argument; fix by passing price when calling. -> Option D
Quick Check:
Factory args must match call args [OK]
Hint: Match factory function parameters with call arguments [OK]
Common Mistakes:
Calling factory with fewer arguments than defined
Returning dict directly instead of function
Misusing fixture as a simple variable
5. You want to create a factory fixture that builds user dictionaries with optional age and default country='USA'. Which of the following implementations correctly achieves this?
Step 1: Understand factory fixture with optional/default parameters
The factory fixture should return a function that accepts parameters with defaults for optional values.
Step 2: Evaluate each option
@pytest.fixture
def user_factory():
def create_user(name, age=None, country='USA'):
return {'name': name, 'age': age, 'country': country}
return create_user correctly defines a fixture returning a function with default age=None and country='USA'. @pytest.fixture
def user_factory(name, age=None, country='USA'):
return {'name': name, 'age': age, 'country': country} is not a factory fixture because it takes parameters directly. @pytest.fixture
def user_factory():
return {'name': 'default', 'age': None, 'country': 'USA'} returns a fixed dict, not a factory. @pytest.fixture
def user_factory():
def create_user(name, age, country):
return {'name': name, 'age': age, 'country': country}
return create_user requires all parameters without defaults, so age and country are not optional.