Skip to content

Commit 58933aa

Browse files
committed
try to better handle @unittest.expectedFailure decorator
1 parent e643e99 commit 58933aa

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Changese between 2.2.3 and ...
33

44
- fix issue 126: correctly match all invalid xml characters for junitxml
55
binary escape
6+
- fix issue with unittest: now @unittest.expectedFailure markers should
7+
be processed correctly (you can also use @pytest.mark markers)
68
- document integration with the extended distribute/setuptools test commands
79

810

_pytest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#
2-
__version__ = '2.2.4.dev1'
2+
__version__ = '2.2.4.dev2'

_pytest/skipping.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def check_xfail_no_run(item):
132132
def pytest_runtest_makereport(__multicall__, item, call):
133133
if not isinstance(item, pytest.Function):
134134
return
135+
# unitttest special case, see setting of _unexpectedsuccess
136+
if hasattr(item, '_unexpectedsuccess'):
137+
rep = __multicall__.execute()
138+
if rep.when == "call":
139+
# we need to translate into how py.test encodes xpass
140+
rep.keywords['xfail'] = "reason: " + item._unexpectedsuccess
141+
rep.outcome = "failed"
142+
return rep
135143
if not (call.excinfo and
136144
call.excinfo.errisinstance(py.test.xfail.Exception)):
137145
evalxfail = getattr(item, '_evalxfail', None)

_pytest/unittest.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,28 @@ def addError(self, testcase, rawexcinfo):
9191
self._addexcinfo(rawexcinfo)
9292
def addFailure(self, testcase, rawexcinfo):
9393
self._addexcinfo(rawexcinfo)
94+
9495
def addSkip(self, testcase, reason):
9596
try:
9697
pytest.skip(reason)
9798
except pytest.skip.Exception:
9899
self._addexcinfo(sys.exc_info())
99-
def addExpectedFailure(self, testcase, rawexcinfo, reason):
100+
101+
def addExpectedFailure(self, testcase, rawexcinfo, reason=""):
100102
try:
101103
pytest.xfail(str(reason))
102104
except pytest.xfail.Exception:
103105
self._addexcinfo(sys.exc_info())
104-
def addUnexpectedSuccess(self, testcase, reason):
105-
pass
106+
107+
def addUnexpectedSuccess(self, testcase, reason=""):
108+
self._unexpectedsuccess = reason
109+
106110
def addSuccess(self, testcase):
107111
pass
112+
108113
def stopTest(self, testcase):
109114
pass
115+
110116
def runtest(self):
111117
self._testcase(result=self)
112118

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def main():
2424
name='pytest',
2525
description='py.test: simple powerful testing with Python',
2626
long_description = long_description,
27-
version='2.2.4.dev1',
27+
version='2.2.4.dev2',
2828
url='http://pytest.org',
2929
license='MIT license',
3030
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],

testing/test_unittest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,24 @@ def test_hello(self, arg1):
459459
result = testdir.runpytest()
460460
assert "TypeError" in result.stdout.str()
461461
assert result.ret == 1
462+
463+
@pytest.mark.skipif("sys.version_info < (2,7)")
464+
def test_unittest_unexpected_failure(testdir):
465+
testdir.makepyfile("""
466+
import unittest
467+
class MyTestCase(unittest.TestCase):
468+
@unittest.expectedFailure
469+
def test_func1(self):
470+
assert 0
471+
@unittest.expectedFailure
472+
def test_func2(self):
473+
assert 1
474+
""")
475+
result = testdir.runpytest("-rxX")
476+
result.stdout.fnmatch_lines([
477+
"*XFAIL*MyTestCase*test_func1*",
478+
"*XPASS*MyTestCase*test_func2*",
479+
"*1 xfailed*1 xpass*",
480+
])
481+
482+

0 commit comments

Comments
 (0)