Bird
0
0

How can you mark a test as expected to fail only on Python 3.12 and above using @pytest.mark.xfail?

hard🚀 Application Q9 of 15
PyTest - Markers
How can you mark a test as expected to fail only on Python 3.12 and above using @pytest.mark.xfail?
A@pytest.mark.xfail(condition=sys.version_info >= (3, 12))
B@pytest.mark.xfail(sys.version_info >= (3, 12))
C@pytest.mark.xfail(lambda: sys.version_info >= (3, 12))
D@pytest.mark.xfail(condition=sys.version_info < (3, 12))
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional xfail

    pytest allows condition argument to mark xfail only if condition is True.
  2. Step 2: Correct syntax

    Use @pytest.mark.xfail(condition=expression) where expression is boolean.
  3. Step 3: Check options

    @pytest.mark.xfail(condition=sys.version_info >= (3, 12)) correctly uses condition= with boolean expression; others are invalid syntax or wrong logic.
  4. Final Answer:

    @pytest.mark.xfail(condition=sys.version_info >= (3, 12)) -> Option A
  5. Quick Check:

    Use condition= for conditional xfail [OK]
Quick Trick: Use condition= to apply xfail conditionally [OK]
Common Mistakes:
MISTAKES
  • Passing condition as positional argument
  • Using lambda instead of boolean expression
  • Using wrong condition logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes