Bird
0
0

Which of the following is the correct way to parametrize a test function with two parameters a and b?

easy🧠 Conceptual Q2 of 15
PyTest - Parametrize
Which of the following is the correct way to parametrize a test function with two parameters a and b?
A@pytest.mark.parametrize('a', [1, 2]) @pytest.mark.parametrize('b', [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])
Step-by-Step Solution
Solution:
  1. Step 1: Check the syntax for multiple parameters

    The correct syntax uses a string with parameter names separated by commas and a list of tuples for values.
  2. Step 2: Validate each option

    @pytest.mark.parametrize('a,b', [(1, 2), (3, 4)]) matches the correct syntax. @pytest.mark.parametrize(['a', 'b'], [1, 2, 3, 4]) incorrectly uses a list of strings and a flat list of values. @pytest.mark.parametrize('a,b', [1, 2, 3, 4]) uses a flat list instead of tuples. @pytest.mark.parametrize('a', [1, 2]) @pytest.mark.parametrize('b', [3, 4]) applies two separate parametrize decorators, which is valid but changes test combinations.
  3. Final Answer:

    @pytest.mark.parametrize('a,b', [(1, 2), (3, 4)]) -> Option B
  4. Quick Check:

    Parametrize syntax = string names + list of tuples [OK]
Quick Trick: Use string names and list of tuples for multiple params [OK]
Common Mistakes:
MISTAKES
  • Passing flat lists instead of list of tuples
  • Using list of strings for parameter names
  • Applying multiple parametrize decorators without understanding combinations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes