0
0
PyTesttesting~20 mins

@pytest.mark.xfail for expected failures - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Xfail Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AThe test is reported as failed.
BThe test is reported as passed.
CThe test is reported as xfailed (expected failure).
DThe test is skipped.
Attempts:
2 left
💡 Hint
Think about what @pytest.mark.xfail means when the test fails.
assertion
intermediate
2: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
AThe test is reported as xpassed (unexpected pass).
BThe test is reported as passed.
CThe test is reported as failed.
DThe test is skipped.
Attempts:
2 left
💡 Hint
What does strict=True do when the test passes?
🔧 Debug
advanced
2: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')
ABecause the test has multiple assertions and only the last fails, pytest reports failure.
BBecause the test raises an unexpected exception type (not AssertionError), pytest reports it as failed, not xfailed.
CBecause the first assertion passes, pytest ignores the xfail mark.
DBecause the test fails, it should be reported as xfailed, so this is a pytest bug.
Attempts:
2 left
💡 Hint
Consider what kinds of failures @pytest.mark.xfail expects.
🧠 Conceptual
advanced
2:00remaining
Purpose of @pytest.mark.xfail in test suites
What is the main purpose of using @pytest.mark.xfail in a test suite?
ATo mark tests that are expected to fail due to known bugs or incomplete features.
BTo skip tests that are not ready to run yet.
CTo mark tests that should always pass regardless of code changes.
DTo mark tests that should run slower for performance reasons.
Attempts:
2 left
💡 Hint
Think about tests that fail but you want to keep in the suite.
framework
expert
2: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
AThe test is reported as passed even if it fails.
BThe test is skipped because the condition is False.
CThe test is reported as xfailed regardless of the condition.
DThe test runs normally and failure is reported as a failure, not xfail.
Attempts:
2 left
💡 Hint
What does the condition parameter control in xfail?