Option A correctly lists parameters as a string and values as tuples.
Explain how to use multiple parameters in pytest's parametrize decorator with an example.
Think about how to run the same test with different inputs.
You got /3 concepts.
What are the benefits and common pitfalls of using multiple parameters in pytest tests?
Consider why this feature is useful and what errors to watch for.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using @pytest.mark.parametrize with multiple parameters in pytest?
easy
A. To run the same test function with different sets of input values
B. To skip tests conditionally based on input values
C. To group multiple test functions into one
D. To generate test reports automatically
Solution
Step 1: Understand the role of @pytest.mark.parametrize
This decorator allows running a test multiple times with different inputs.
Step 2: Recognize the benefit of multiple parameters
Using multiple parameters lets you test combinations of inputs easily in one test function.
Final Answer:
To run the same test function with different sets of input values -> Option A
Quick Check:
Multiple parameters = multiple input sets [OK]
Hint: Parametrize runs tests repeatedly with different inputs [OK]
Common Mistakes:
Thinking it skips tests instead of running multiple times
Confusing it with grouping tests
Assuming it generates reports automatically
2. Which of the following is the correct syntax to parametrize a test function with two parameters a and b in pytest?
easy
A. @pytest.mark.parametrize((a, b), [(1, 2), (3, 4)])
B. @pytest.mark.parametrize('a, b', [(1, 2), (3, 4)])
C. @pytest.mark.parametrize([a, b], [(1, 2), (3, 4)])
D. @pytest.mark.parametrize('a b', [(1, 2), (3, 4)])
Solution
Step 1: Recall the correct parameter names format
Pytest expects a string with parameter names separated by commas, e.g., 'a, b'.
Step 2: Check the value list format
Values must be a list of tuples matching the parameters, e.g., [(1, 2), (3, 4)].
Final Answer:
@pytest.mark.parametrize('a, b', [(1, 2), (3, 4)]) -> Option B
Quick Check:
Parameter names as string with commas [OK]
Hint: Use a string with comma-separated names for parameters [OK]
Common Mistakes:
Using tuple or list instead of string for parameter names
Missing commas between parameter names
Using space instead of comma to separate names
3. What will be the output when running this pytest test?
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. First test fails, second test passes
Solution
Step 1: Evaluate first test with inputs (1, 2)
Sum is 1 + 2 = 3, assertion 3 == 3 passes.
Step 2: Evaluate second test with inputs (3, 4)
Sum is 3 + 4 = 7, assertion 7 == 3 fails.
Final Answer:
First test passes, second test fails -> Option A
Quick Check:
Check sums against 3 [OK]
Hint: Calculate sums for each tuple and compare [OK]
Common Mistakes:
Assuming all tests pass without checking values
Ignoring that assertion compares to fixed number 3
Confusing which test input causes failure
4. Identify the error in this pytest code using multiple parameters:
import pytest
@pytest.mark.parametrize('a, b', [1, 2, 3])
def test_multiply(a, b):
assert a * b > 0
medium
A. The test function must return a value
B. The parameter names should be a list, not a string
C. The assertion syntax is incorrect
D. The parameter values should be tuples, not integers
Solution
Step 1: Check the parameter values format
Values must be a list of tuples matching parameters (a, b), but here it's a list of integers.
Step 2: Understand why this causes error
Pytest tries to unpack each integer into two variables, causing a TypeError.
Final Answer:
The parameter values should be tuples, not integers -> Option D
Quick Check:
Values must match parameter count [OK]
Hint: Use tuples for multiple parameters values [OK]
Common Mistakes:
Passing a flat list instead of list of tuples
Thinking parameter names must be a list
Expecting test function to return a value
5. You want to test a function is_even_sum(a, b) that returns True if the sum of a and b is even. Which @pytest.mark.parametrize setup with a single decorator correctly tests all combinations of a from [1, 2] and b from [3, 4] respectively?
hard
A. @pytest.mark.parametrize('a', [1, 2])
@pytest.mark.parametrize('b', [3, 4])
B. @pytest.mark.parametrize('a, b', [(1, 2), (3, 4)])