From 62b8712ca9d8d1b01df1d1a505de7597a9cfb530 Mon Sep 17 00:00:00 2001 From: Floris Bruynooghe Date: Tue, 23 Sep 2014 23:55:26 +0100 Subject: [PATCH] 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. --- _pytest/skipping.py | 5 +---- testing/test_skipping.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/_pytest/skipping.py b/_pytest/skipping.py index 56f28b118..188db6122 100644 --- a/_pytest/skipping.py +++ b/_pytest/skipping.py @@ -58,6 +58,7 @@ class MarkEvaluator: @property def holder(self): return self.item.keywords.get(self.name, None) + def __bool__(self): return bool(self.holder) __nonzero__ = __bool__ @@ -136,8 +137,6 @@ class MarkEvaluator: @pytest.mark.tryfirst def pytest_runtest_setup(item): - if not isinstance(item, pytest.Function): - return evalskip = MarkEvaluator(item, 'skipif') if evalskip.istrue(): pytest.skip(evalskip.getexplanation()) @@ -155,8 +154,6 @@ def check_xfail_no_run(item): pytest.xfail("[NOTRUN] " + evalxfail.getexplanation()) def pytest_runtest_makereport(__multicall__, item, call): - if not isinstance(item, pytest.Function): - return # unitttest special case, see setting of _unexpectedsuccess if hasattr(item, '_unexpectedsuccess'): rep = __multicall__.execute() diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 45652703a..1425b88ef 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -678,3 +678,23 @@ class TestBooleanCondition: *True123* *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