Bird
0
0

You have a test that intermittently fails due to a known issue. You want pytest to mark it as expected failure but fail the test suite if it unexpectedly passes. Which decorator usage is correct?

hard🚀 Application Q8 of 15
PyTest - Markers
You have a test that intermittently fails due to a known issue. You want pytest to mark it as expected failure but fail the test suite if it unexpectedly passes. Which decorator usage is correct?
A@pytest.mark.xfail(strict=False)
B@pytest.mark.xfail(strict=True)
C@pytest.mark.skipif(condition=True)
D@pytest.mark.xfail(reason='flaky test')
Step-by-Step Solution
Solution:
  1. Step 1: Understand strict parameter

    strict=True causes pytest to fail the suite if an xfail test unexpectedly passes.
  2. Step 2: Identify correct decorator

    Using @pytest.mark.xfail(strict=True) marks the test as expected failure but enforces failure on unexpected pass.
  3. Step 3: Evaluate other options

    strict=False allows unexpected passes without failing suite; skipif skips test; reason only adds explanation.
  4. Final Answer:

    @pytest.mark.xfail(strict=True) -> Option B
  5. Quick Check:

    Use strict=True to fail on unexpected pass [OK]
Quick Trick: strict=True fails suite if xfail test passes unexpectedly [OK]
Common Mistakes:
MISTAKES
  • Using strict=False when strict=True is needed
  • Confusing skipif with xfail
  • Assuming reason parameter affects strictness

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes