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
✗ Incorrect
The decorator runs the same test function multiple times with different sets of parameters.
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
✗ Incorrect
You specify parameter names as a string and provide a list of tuples with values for each test run.
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
✗ Incorrect
Each parameter set runs as a separate test case; failure in one does not stop others.
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')
✗ Incorrect
Option C correctly specifies parameter names and a list of tuples with values.
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
✗ Incorrect
It helps write fewer tests by reusing the same test logic with different data.
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.
Practice
(1/5)
1. What is the main purpose of the @pytest.mark.parametrize decorator in pytest?
easy
A. To skip a test function during test execution
B. To run the same test function multiple times with different input values
C. To mark a test as expected to fail
D. To group multiple test functions into a test suite
Solution
Step 1: Understand the decorator's role
@pytest.mark.parametrize allows running one test function multiple times with different inputs.
Step 2: Compare options with this role
Only To run the same test function multiple times with different input values describes running the same test with different inputs, matching the decorator's purpose.
Final Answer:
To run the same test function multiple times with different input values -> Option B
Quick Check:
Parametrize = multiple inputs [OK]
Hint: Parametrize means run test many times with different data [OK]
Common Mistakes:
Confusing parametrize with skipping tests
Thinking it groups tests instead of repeating one
Mixing it up with marking expected failures
2. Which of the following is the correct syntax to use @pytest.mark.parametrize for testing a function with inputs 1 and 2?
easy
A. @pytest.mark.parametrized('input', [1, 2])
B. @pytest.parametrize('input', (1, 2))
C. @pytest.mark.parametrize(input, [1, 2])
D. @pytest.mark.parametrize('input', [1, 2])
Solution
Step 1: Check correct decorator name and syntax
The correct decorator is @pytest.mark.parametrize with parameters as strings and list of values.
Step 2: Validate each option
@pytest.mark.parametrize('input', [1, 2]) uses correct spelling, quotes around parameter name, and list syntax. Others have syntax errors or wrong names.
Final Answer:
@pytest.mark.parametrize('input', [1, 2]) -> Option D
Quick Check:
Correct spelling and syntax = @pytest.mark.parametrize('input', [1, 2]) [OK]
Hint: Decorator name is mark.parametrize with string param name [OK]
Common Mistakes:
Misspelling 'parametrize' as 'parametrized'
Forgetting quotes around parameter name
Using wrong brackets or missing @ symbol
3. What will be the output when running this pytest code?
import pytest
@pytest.mark.parametrize('x,y', [(1,2), (3,4)])
def test_sum(x, y):
assert x + y == 3
medium
A. First test passes, second test fails
B. Both tests fail
C. Both tests pass
D. Syntax error during test collection
Solution
Step 1: Analyze test cases and assertion
Test runs twice: (1,2) and (3,4). Assertion checks if sum equals 3.
Step 2: Evaluate each case
1+2=3 passes; 3+4=7 fails assertion.
Final Answer:
First test passes, second test fails -> Option A
Quick Check:
Sum check true only for (1,2) [OK]
Hint: Check each input pair sum against assertion [OK]
Common Mistakes:
Assuming all tests pass without checking values
Confusing tuple unpacking in parametrize
Ignoring assertion logic
4. Identify the error in this pytest code using @pytest.mark.parametrize:
Test function must accept the parameter named in parametrize ('num'), but test_even() has no parameters.
Step 2: Confirm other parts
Decorator name and list syntax are correct; assertion logic is valid for even check.
Final Answer:
Missing parameter in test function definition -> Option C
Quick Check:
Function must accept parametrize variables [OK]
Hint: Test function needs parameters matching parametrize names [OK]
Common Mistakes:
Forgetting to add parameters to test function
Thinking list must be tuple
Confusing assertion logic with syntax error
5. You want to test a function is_valid_age(age) that returns True if age is between 18 and 65 inclusive. Which @pytest.mark.parametrize usage correctly tests ages 17, 18, 30, 65, and 70 with expected results?