0
0
PyTesttesting~5 mins

Parametrized fixtures in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a parametrized fixture in pytest?
A parametrized fixture is a fixture that runs multiple times with different input values, allowing tests to reuse setup code with various data.
Click to reveal answer
beginner
How do you define a parametrized fixture in pytest?
Use the @pytest.fixture decorator with the 'params' argument listing values. pytest runs the fixture once for each value.
Click to reveal answer
beginner
Example: What does this fixture do?
@pytest.fixture(params=[1, 2, 3])
def number(request):
    return request.param
It creates a fixture named 'number' that will provide values 1, 2, and 3 to tests, running each test three times with these values.
Click to reveal answer
intermediate
Why use parametrized fixtures instead of multiple test functions?
Parametrized fixtures reduce code duplication and keep tests clean by reusing setup logic with different inputs automatically.
Click to reveal answer
beginner
How can you access the current parameter value inside a parametrized fixture?
Use 'request.param' inside the fixture function to get the current parameter value.
Click to reveal answer
What does the 'params' argument in @pytest.fixture do?
ARuns the fixture multiple times with each value in the list
BSkips the fixture
CRuns the fixture only once
DMarks the fixture as private
How do you get the current parameter value inside a parametrized fixture?
AUsing 'param.value'
BUsing 'self.param'
CUsing 'fixture.param'
DUsing 'request.param'
If a fixture is parametrized with 3 values, how many times will a test using it run?
A1
BDepends on test count
C3
D0
Which decorator is used to create a parametrized fixture?
A@pytest.run
B@pytest.fixture(params=[...])
C@pytest.test
D@pytest.parametrize
What is a benefit of parametrized fixtures?
AThey reduce code duplication
BThey slow down tests
CThey hide test failures
DThey disable tests
Explain how to create and use a parametrized fixture in pytest.
Think about how pytest runs tests multiple times with different data.
You got /3 concepts.
    Describe the advantages of using parametrized fixtures over writing multiple test functions.
    Consider how setup code can be reused.
    You got /3 concepts.