Let xfail work on non-python Items

For some reason xfail was only implemented on non-python Item
instances.  This removes this guard which means plugins creating new
items can raise pytest.xfail.Exception and it will work as expected.
This commit is contained in:
Floris Bruynooghe 2014-09-23 23:55:26 +01:00
parent 68105b3ae4
commit 62b8712ca9
2 changed files with 21 additions and 4 deletions

View File

@ -58,6 +58,7 @@ class MarkEvaluator:
@property @property
def holder(self): def holder(self):
return self.item.keywords.get(self.name, None) return self.item.keywords.get(self.name, None)
def __bool__(self): def __bool__(self):
return bool(self.holder) return bool(self.holder)
__nonzero__ = __bool__ __nonzero__ = __bool__
@ -136,8 +137,6 @@ class MarkEvaluator:
@pytest.mark.tryfirst @pytest.mark.tryfirst
def pytest_runtest_setup(item): def pytest_runtest_setup(item):
if not isinstance(item, pytest.Function):
return
evalskip = MarkEvaluator(item, 'skipif') evalskip = MarkEvaluator(item, 'skipif')
if evalskip.istrue(): if evalskip.istrue():
pytest.skip(evalskip.getexplanation()) pytest.skip(evalskip.getexplanation())
@ -155,8 +154,6 @@ def check_xfail_no_run(item):
pytest.xfail("[NOTRUN] " + evalxfail.getexplanation()) pytest.xfail("[NOTRUN] " + evalxfail.getexplanation())
def pytest_runtest_makereport(__multicall__, item, call): def pytest_runtest_makereport(__multicall__, item, call):
if not isinstance(item, pytest.Function):
return
# unitttest special case, see setting of _unexpectedsuccess # unitttest special case, see setting of _unexpectedsuccess
if hasattr(item, '_unexpectedsuccess'): if hasattr(item, '_unexpectedsuccess'):
rep = __multicall__.execute() rep = __multicall__.execute()

View File

@ -678,3 +678,23 @@ class TestBooleanCondition:
*True123* *True123*
*1 xfail* *1 xfail*
""") """)
def test_xfail_item(testdir):
# Ensure pytest.xfail works with non-Python Item
testdir.makeconftest("""
import pytest
class MyItem(pytest.Item):
nodeid = 'foo'
def runtest(self):
pytest.xfail("Expected Failure")
def pytest_collect_file(path, parent):
return MyItem("foo", parent)
""")
result = testdir.inline_run()
passed, skipped, failed = result.listoutcomes()
assert not failed
xfailed = [r for r in skipped if hasattr(r, 'wasxfail')]
assert xfailed