0
0
PyTesttesting~10 mins

@pytest.mark.xfail for expected failures - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark the test as expected to fail using pytest.

PyTest
import pytest

@pytest.mark.[1]
def test_example():
    assert 1 == 2
Drag options to blanks, or click blank then click option'
Askip
Bparametrize
Cxfail
Dfixture
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.mark.skip instead of @pytest.mark.xfail
Forgetting to add the decorator
Using @pytest.mark.fixture which is for setup, not expected failures
2fill in blank
medium

Complete the code to mark the test as expected to fail only if the condition is True.

PyTest
import pytest

@pytest.mark.xfail(condition=[1])
def test_conditional():
    assert 0 == 1
Drag options to blanks, or click blank then click option'
ATrue
BFalse
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or None which are falsy and will not mark the test as expected fail
Using False which disables the xfail
3fill in blank
hard

Fix the error in the code to correctly mark the test as expected to fail with a reason.

PyTest
import pytest

@pytest.mark.xfail(reason=[1])
def test_reason():
    assert False
Drag options to blanks, or click blank then click option'
AKnown bug
B"Known bug"
C'Known bug
DKnown bug"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the reason string
Using mismatched quotes causing syntax errors
4fill in blank
hard

Fill both blanks to mark the test as expected to fail only on Python 3.12 and provide a reason.

PyTest
import pytest
import sys

@pytest.mark.xfail(sys.version_info[:2] == [1], reason=[2])
def test_version():
    assert False
Drag options to blanks, or click blank then click option'
A(3, 12)
B"Fails on 3.12"
C'Fails on 3.12'
D(3, 11)
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong version tuple
Not quoting the reason string
Using mismatched quotes
5fill in blank
hard

Fill all three blanks to mark the test as expected to fail with a reason and strict mode enabled.

PyTest
import pytest

@pytest.mark.xfail(condition=[1], reason=[2], strict=[3])
def test_strict():
    assert False
Drag options to blanks, or click blank then click option'
ATrue
B"Strict mode enabled"
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using False for condition which disables xfail
Not quoting the reason string
Using False for strict which allows unexpected passes