Labs ICT
โญ Pro Login

Skip & XFail Marks

Conditionally skipping tests or marking them as expected failures.

Skip and XFail Marks

Sometimes you need to conditionally skip a test or mark it as an expected failure. pytest provides skip and xfail for these scenarios.

Skipping Tests

Use @pytest.mark.skip to unconditionally skip a test:

@pytest.mark.skip(reason="Feature not implemented yet")
def test_future_feature():
    pass

# Or skip conditionally
@pytest.mark.skipif(
    sys.platform == "win32",
    reason="Does not work on Windows"
)
def test_unix_only():
    pass

Skipping Inside a Test

You can skip a test mid-execution using pytest.skip():

def test_dynamic_skip():
    user = get_current_user()
    if user is None:
        pytest.skip("No user logged in")
    assert user.is_active

Expected Failures (xfail)

Use @pytest.mark.xfail when you know a test will fail:

@pytest.mark.xfail(reason="Known bug #456")
def test_known_bug():
    assert broken_function() == expected_value

# The test still runs; if it PASSES, pytest reports it as "xpass"

Strict xfail

By default, xfail tests that unexpectedly pass are reported as xpass. Use strict=True to make them fail instead:

@pytest.mark.xfail(reason="Bug exists", strict=True)
def test_should_fail():
    # If this accidentally passes, the test suite fails
    assert buggy_code()

๐Ÿงช Quick Quiz

What does @pytest.mark.xfail indicate?