Bird
0
0

You want to test a function with parameters x and y where x takes values 1 and 2, and y takes values 3 and 4. How do you parametrize the test to run all combinations of x and y?

hard🚀 Application Q8 of 15
PyTest - Parametrize
You want to test a function with parameters x and y where x takes values 1 and 2, and y takes values 3 and 4. How do you parametrize the test to run all combinations of x and y?
AUse a single <code>@pytest.mark.parametrize</code> with pairs [(1,3), (2,4)]
BUse two separate <code>@pytest.mark.parametrize</code> decorators, one for <code>x</code> and one for <code>y</code>
CUse a single <code>@pytest.mark.parametrize</code> with pairs [(1,4), (2,3)]
DUse a single <code>@pytest.mark.parametrize</code> with a list of single values [1, 2, 3, 4]
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to generate all combinations

    Using two separate parametrize decorators creates a Cartesian product of parameters, testing all combinations.
  2. Step 2: Evaluate options

    Use two separate @pytest.mark.parametrize decorators, one for x and one for y correctly uses two decorators to get all combinations: (1,3), (1,4), (2,3), (2,4). Options B and C only test two pairs, missing combinations. Use a single @pytest.mark.parametrize with a list of single values [1, 2, 3, 4] is invalid syntax.
  3. Final Answer:

    Use two separate @pytest.mark.parametrize decorators, one for x and one for y -> Option B
  4. Quick Check:

    Two decorators = all combinations [OK]
Quick Trick: Stack parametrize decorators for all combos [OK]
Common Mistakes:
MISTAKES
  • Passing only pairs missing some combinations
  • Using flat lists for multiple parameters
  • Expecting single decorator to generate all combos

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes