0
0
PyTesttesting~5 mins

Fixture factories in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA fixture function
BA test case
CA test report
DA test runner
Why use fixture factories instead of multiple similar fixtures?
ATo reduce code duplication and improve reuse
BTo slow down test execution
CTo make tests harder to read
DTo avoid using pytest
Which decorator is used to mark a fixture in pytest?
A@pytest.run
B@pytest.fixture
C@pytest.test
D@pytest.assert
How can you pass parameters to a fixture created by a fixture factory?
ABy modifying pytest.ini
BBy using global variables
CBy calling the factory function with arguments
DBy using command line options only
What is a common use case for fixture factories?
AInstalling pytest plugins
BRunning tests in parallel
CGenerating test reports
DCreating test data objects with different attributes
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.