Bird
0
0

Which approach correctly uses ids to help identify the skipped test case?

hard🚀 Application Q15 of 15
PyTest - Parametrize
You want to parametrize a test with inputs [(0, 'zero'), (1, 'one'), (2, 'two')] but want to skip the test case where the number is zero using IDs. Which approach correctly uses ids to help identify the skipped test case?
A@pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=lambda val: f"skip_{val[0]}" if val[0]==0 else f"num_{val[0]}")
B@pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['skip_zero', 'num_one', 'num_two'])
C@pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['zero', 'one', 'two'])
D@pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['num_0', 'num_1', 'num_2'])
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal of IDs for skipping

    We want to label the zero case clearly as 'skip_zero' to identify it easily in reports.
  2. Step 2: Evaluate each option's IDs usage

    @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['skip_zero', 'num_one', 'num_two']) uses a list of strings with 'skip_zero' for zero, which is clear and correct. The lambda in @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=lambda val: f"skip_{val[0]}" if val[0]==0 else f"num_{val[0]}") is corrected to accept a single tuple argument as pytest passes. The options with ids=['num_0', 'num_1', 'num_2'] and ids=['zero', 'one', 'two'] do not label zero as skipped.
  3. Final Answer:

    @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['skip_zero', 'num_one', 'num_two']) -> Option B
  4. Quick Check:

    Use explicit ID list to mark skipped case [OK]
Quick Trick: Use explicit ID list to label skipped cases [OK]
Common Mistakes:
MISTAKES
  • Using lambda incorrectly for IDs
  • Not labeling skipped cases clearly
  • Using generic IDs without skip info

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes