Bird
0
0

You want to mark a test that should be skipped on Windows and expected to fail on Python versions below 3.7. Which is the correct way to combine skipif and xfail markers?

hard📝 framework Q15 of 15
PyTest - Markers
You want to mark a test that should be skipped on Windows and expected to fail on Python versions below 3.7. Which is the correct way to combine skipif and xfail markers?
A@pytest.mark.xfail(sys.version_info < (3,7, reason='Expected fail on old Python')\n@pytest.mark.skipif(sys.platform == 'win32', reason='Skip on Windows')\ndef test_func(): pass
B@pytest.mark.skipif(sys.platform == 'win32' and sys.version_info < (3,7))\ndef test_func(): pass
C@pytest.mark.skipif(sys.platform == 'win32', reason='Skip on Windows')\n@pytest.mark.xfail(sys.version_info < (3,7), reason='Expected fail on old Python')\ndef test_func(): pass
D@pytest.mark.skipif(sys.platform == 'win32')\n@pytest.mark.skipif(sys.version_info < (3,7))\ndef test_func(): pass
Step-by-Step Solution
Solution:
  1. Step 1: Understand marker order and logic

    Tests should be skipped on Windows unconditionally, so skipif for Windows must be applied first.
  2. Step 2: Apply xfail conditionally for Python versions below 3.7

    The xfail marker is applied second to mark expected failures on old Python versions.
  3. Step 3: Verify option correctness

    @pytest.mark.skipif(sys.platform == 'win32', reason='Skip on Windows')\n@pytest.mark.xfail(sys.version_info < (3,7), reason='Expected fail on old Python')\ndef test_func(): pass applies skipif first, then xfail, both with reasons. This is the correct and clear approach.
  4. Final Answer:

    Use skipif for Windows, then xfail for Python version condition with reasons. -> Option C
  5. Quick Check:

    Skip first, then xfail with reasons = B [OK]
Quick Trick: Apply skipif first, then xfail with clear reasons [OK]
Common Mistakes:
MISTAKES
  • Reversing marker order causing unexpected runs
  • Combining conditions incorrectly in one skipif
  • Omitting reason strings for markers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes