0
0
PyTesttesting~5 mins

Factory fixtures in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
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
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
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
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.