0
0
PyTesttesting~5 mins

@pytest.mark.parametrize decorator - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @pytest.mark.parametrize decorator in pytest?
It allows you to run the same test function multiple times with different input values, making tests more efficient and easier to maintain.
Click to reveal answer
beginner
How do you specify multiple sets of input values using @pytest.mark.parametrize?
You provide a string of comma-separated parameter names and a list of tuples with values for each test run.
Click to reveal answer
beginner
Example: What will happen when this test runs?<br><pre>import pytest

@pytest.mark.parametrize('x, y, expected', [(1, 2, 3), (4, 5, 9)])
def test_add(x, y, expected):
    assert x + y == expected</pre>
The test test_add will run twice: once with (1, 2, 3) and once with (4, 5, 9). Both runs check if x + y equals expected.
Click to reveal answer
intermediate
Can @pytest.mark.parametrize be used to test edge cases and why is this useful?
Yes, it helps test many edge cases easily by running the same test with different inputs, improving test coverage without writing many separate tests.
Click to reveal answer
intermediate
What happens if one of the parameter sets in @pytest.mark.parametrize causes the test to fail?
Only that specific test run fails, while others continue. This helps identify which input caused the failure clearly.
Click to reveal answer
What does @pytest.mark.parametrize do?
AGroups tests into a suite
BSkips a test
CMarks a test as expected to fail
DRuns a test multiple times with different inputs
How do you pass parameters to @pytest.mark.parametrize?
AAs environment variables
BAs a string of parameter names and a list of tuples with values
CAs a single list of values
DAs a dictionary of key-value pairs
If a test with @pytest.mark.parametrize fails for one input set, what happens?
AOnly that input set's test fails; others run normally
BAll tests stop immediately
CAll tests fail
DThe test is skipped
Which of these is a correct use of @pytest.mark.parametrize?
A@pytest.mark.parametrize([1,2,3])
B@pytest.mark.parametrize('a', 5)
C@pytest.mark.parametrize('a,b', [(1,2), (3,4)])
D@pytest.mark.parametrize('a,b,c')
Why is @pytest.mark.parametrize helpful for testing?
AIt reduces code duplication by running one test with many inputs
BIt makes tests run faster by skipping some
CIt automatically fixes bugs
DIt hides test failures
Explain how to use @pytest.mark.parametrize to test a function with multiple input sets.
Think about how you tell pytest which inputs to use for each test run.
You got /5 concepts.
    Describe the benefits of using @pytest.mark.parametrize in your test suite.
    Consider how it helps when you want to test many input values without writing many tests.
    You got /5 concepts.