It tells pytest to skip the test unconditionally, so the test will not run at all.
Click to reveal answer
beginner
How does @pytest.mark.skipif(condition) work?
It skips the test only if the given condition is true. If false, the test runs normally.
Click to reveal answer
beginner
What is the purpose of @pytest.mark.xfail?
It marks a test as expected to fail. If the test fails, pytest reports it as an expected failure, not a test failure.
Click to reveal answer
intermediate
When would you use skipif instead of skip?
Use skipif when you want to skip a test only under certain conditions, like missing dependencies or specific OS.
Click to reveal answer
intermediate
What happens if a test marked with xfail passes?
Pytest reports it as an unexpected pass, which means the test was expected to fail but did not.
Click to reveal answer
What does @pytest.mark.skip do?
ARuns the test only if a condition is true
BMarks the test as expected to fail
CSkips the test unconditionally
DRuns the test twice
✗ Incorrect
skip skips the test no matter what.
When does @pytest.mark.skipif(condition) skip a test?
AOnly if the condition is true
BNever
COnly if the condition is false
DAlways
✗ Incorrect
skipif skips only when the condition is true.
What does @pytest.mark.xfail indicate?
ATest should be skipped
BTest is expected to fail
CTest must pass twice
DTest is flaky
✗ Incorrect
xfail marks tests expected to fail.
If a test marked with xfail passes, pytest reports it as:
ASkipped
BFail
CPass
DUnexpected pass
✗ Incorrect
Passing an xfail test is an unexpected pass.
Which marker would you use to skip a test only on Windows OS?
A@pytest.mark.skipif(sys.platform == 'win32')
B@pytest.mark.xfail
C@pytest.mark.skip
D@pytest.mark.run
✗ Incorrect
skipif with a condition checks the OS and skips accordingly.
Explain the differences between skip, skipif, and xfail markers in pytest.
Think about when and why you would skip or expect failure.
You got /4 concepts.
Describe a real-life scenario where you would use @pytest.mark.skipif.
Consider tests that depend on external conditions.
You got /3 concepts.
Practice
(1/5)
1. What does the @pytest.mark.skip marker do in a test?
easy
A. It retries the test multiple times before failing.
B. It runs the test only if a condition is true.
C. It marks the test as expected to fail but still runs it.
D. It always skips the test, so it does not run.
Solution
Step 1: Understand the purpose of @pytest.mark.skip
This marker tells pytest to skip the test unconditionally, so the test is not executed at all.
Step 2: Compare with other markers
skipif skips conditionally, and xfail marks expected failures but runs the test. So only skip always skips.
Final Answer:
It always skips the test, so it does not run. -> Option D
Quick Check:
@pytest.mark.skip always skips = D [OK]
Hint: Skip always means test won't run at all [OK]
Common Mistakes:
Confusing skip with skipif (conditional skip)
Thinking xfail skips the test
Assuming skip reruns the test
2. Which of the following is the correct syntax to skip a test only if Python version is less than 3.8?
easy
A. @pytest.mark.skipif(sys.version_info < (3, 8)\ndef test_func(): pass
B. @pytest.mark.skipif(sys.version_info < (3, 8), reason='Python < 3.8')\ndef test_func(): pass
C. @pytest.mark.skipif(sys.version_info < 3.8)\ndef test_func(): pass
D. @pytest.mark.skipif(sys.version_info < '3.8')\ndef test_func(): pass
Solution
Step 1: Check correct version tuple comparison syntax
Python version info is a tuple like (3, 7, 5). So comparisons must use tuples, e.g., (3, 8).
Step 2: Verify correct use of skipif with reason
@pytest.mark.skipif(sys.version_info < (3, 8), reason='Python < 3.8')\ndef test_func(): pass uses tuple comparison and includes a reason string, which is best practice for clarity.
C. The condition syntax is incorrect; should use double equals.
D. The test function must return a value.
Solution
Step 1: Check imports used in condition
The condition uses sys.platform, but sys is not imported, causing a NameError at runtime.
Step 2: Verify other parts
The condition syntax is correct with ==, skipif is valid, and test functions do not need to return values.
Final Answer:
Missing import of sys module causes NameError. -> Option A
Quick Check:
Missing import sys = A [OK]
Hint: Always import modules used in skipif conditions [OK]
Common Mistakes:
Forgetting to import sys
Confusing skipif with skip
Expecting test functions to return values
5. 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
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
Solution
Step 1: Understand marker order and logic
Tests should be skipped on Windows unconditionally, so skipif for Windows must be applied first.
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.
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.
Final Answer:
Use skipif for Windows, then xfail for Python version condition with reasons. -> Option C
Quick Check:
Skip first, then xfail with reasons = B [OK]
Hint: Apply skipif first, then xfail with clear reasons [OK]
Common Mistakes:
Reversing marker order causing unexpected runs
Combining conditions incorrectly in one skipif
Omitting reason strings for markers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.