Recall & Review
beginner
What is a fixture factory in pytest?
A fixture factory is a function that returns a pytest fixture. It allows creating fixtures dynamically with parameters, helping to reuse setup code with different data.
Click to reveal answer
intermediate
How do you create a fixture factory in pytest?
Define a regular function that returns a fixture function. Use @pytest.fixture inside the factory or return a nested function decorated with @pytest.fixture to generate fixtures dynamically.
Click to reveal answer
beginner
Why use fixture factories instead of regular fixtures?
Fixture factories let you create multiple fixtures with different parameters without repeating code. This makes tests cleaner and easier to maintain.Click to reveal answer
intermediate
Example: How to write a fixture factory that creates user objects with different names?
import pytest
def user_factory(name):
@pytest.fixture
def _user():
return {'name': name}
return _user
Use by calling user_factory('Alice') to get a fixture for user named Alice.Click to reveal answer
intermediate
What is the benefit of using fixture factories for parameterized tests?
Fixture factories allow creating fixtures with different parameters easily, which helps run the same test logic with varied data without duplicating fixtures.
Click to reveal answer
What does a fixture factory in pytest return?
✗ Incorrect
A fixture factory returns a fixture function that pytest can use to set up test data or state.
Why use fixture factories instead of multiple similar fixtures?
✗ Incorrect
Fixture factories help reduce repeated code by generating fixtures dynamically with different parameters.
Which decorator is used to mark a fixture in pytest?
✗ Incorrect
The @pytest.fixture decorator marks a function as a fixture in pytest.
How can you pass parameters to a fixture created by a fixture factory?
✗ Incorrect
You pass parameters by calling the fixture factory function with arguments to create customized fixtures.
What is a common use case for fixture factories?
✗ Incorrect
Fixture factories are often used to create test data objects with varying attributes for flexible testing.
Explain how fixture factories improve test code reuse in pytest.
Think about how you can create many similar fixtures without writing each one separately.
You got /4 concepts.
Describe the steps to create and use a fixture factory in pytest with an example.
Consider a simple example like creating user objects with different names.
You got /5 concepts.