Challenge - 5 Problems
Xfail Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a test marked with @pytest.mark.xfail
What will be the test result when running this pytest test marked with @pytest.mark.xfail and the test fails as expected?
PyTest
import pytest @pytest.mark.xfail def test_example(): assert 1 == 2
Attempts:
2 left
💡 Hint
Think about what @pytest.mark.xfail means when the test fails.
✗ Incorrect
When a test marked with @pytest.mark.xfail fails, pytest reports it as xfailed, meaning the failure was expected and does not count as a failure.
❓ assertion
intermediate2:00remaining
Assertion behavior with @pytest.mark.xfail and strict=True
Given this test marked with @pytest.mark.xfail(strict=True), what happens if the test passes unexpectedly?
PyTest
import pytest @pytest.mark.xfail(strict=True) def test_strict_xfail(): assert 1 == 1
Attempts:
2 left
💡 Hint
What does strict=True do when the test passes?
✗ Incorrect
With strict=True, if a test marked xfail passes, pytest reports it as xpassed, indicating an unexpected pass.
🔧 Debug
advanced2:00remaining
Identify why a test marked with @pytest.mark.xfail is reported as failed
Why does this test marked with @pytest.mark.xfail fail instead of being reported as xfailed?
PyTest
import pytest @pytest.mark.xfail(raises=AssertionError) def test_bug(): assert 'a' in 'apple' raise ValueError('bug')
Attempts:
2 left
💡 Hint
Consider what kinds of failures @pytest.mark.xfail expects.
✗ Incorrect
If the test raises an unexpected exception type (not AssertionError), pytest reports it as failed, not xfailed.
🧠 Conceptual
advanced2:00remaining
Purpose of @pytest.mark.xfail in test suites
What is the main purpose of using @pytest.mark.xfail in a test suite?
Attempts:
2 left
💡 Hint
Think about tests that fail but you want to keep in the suite.
✗ Incorrect
The xfail mark is used to indicate tests that are expected to fail, often due to known bugs or unfinished features, so their failures don't break the test suite.
❓ framework
expert2:00remaining
Behavior of @pytest.mark.xfail with condition and reason
Consider this test marked with @pytest.mark.xfail(condition, reason). What happens if the condition is False?
PyTest
import pytest @pytest.mark.xfail(condition=False, reason="Known bug") def test_conditional_xfail(): assert False
Attempts:
2 left
💡 Hint
What does the condition parameter control in xfail?
✗ Incorrect
If the condition is False, the xfail mark is ignored and the test runs normally; failures are reported as failures.