Skip to content

Commit

Permalink
try to better handle @unittest.expectedFailure decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
hpk42 committed Mar 20, 2012
1 parent e643e99 commit 58933aa
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changese between 2.2.3 and ...

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


Expand Down
2 changes: 1 addition & 1 deletion _pytest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#
__version__ = '2.2.4.dev1'
__version__ = '2.2.4.dev2'
8 changes: 8 additions & 0 deletions _pytest/skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ def check_xfail_no_run(item):
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()
if rep.when == "call":
# we need to translate into how py.test encodes xpass
rep.keywords['xfail'] = "reason: " + item._unexpectedsuccess
rep.outcome = "failed"
return rep
if not (call.excinfo and
call.excinfo.errisinstance(py.test.xfail.Exception)):
evalxfail = getattr(item, '_evalxfail', None)
Expand Down
12 changes: 9 additions & 3 deletions _pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,28 @@ def addError(self, testcase, rawexcinfo):
self._addexcinfo(rawexcinfo)
def addFailure(self, testcase, rawexcinfo):
self._addexcinfo(rawexcinfo)

def addSkip(self, testcase, reason):
try:
pytest.skip(reason)
except pytest.skip.Exception:
self._addexcinfo(sys.exc_info())
def addExpectedFailure(self, testcase, rawexcinfo, reason):

def addExpectedFailure(self, testcase, rawexcinfo, reason=""):
try:
pytest.xfail(str(reason))
except pytest.xfail.Exception:
self._addexcinfo(sys.exc_info())
def addUnexpectedSuccess(self, testcase, reason):
pass

def addUnexpectedSuccess(self, testcase, reason=""):
self._unexpectedsuccess = reason

def addSuccess(self, testcase):
pass

def stopTest(self, testcase):
pass

def runtest(self):
self._testcase(result=self)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main():
name='pytest',
description='py.test: simple powerful testing with Python',
long_description = long_description,
version='2.2.4.dev1',
version='2.2.4.dev2',
url='http://pytest.org',
license='MIT license',
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],
Expand Down
21 changes: 21 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,24 @@ def test_hello(self, arg1):
result = testdir.runpytest()
assert "TypeError" in result.stdout.str()
assert result.ret == 1

@pytest.mark.skipif("sys.version_info < (2,7)")
def test_unittest_unexpected_failure(testdir):
testdir.makepyfile("""
import unittest
class MyTestCase(unittest.TestCase):
@unittest.expectedFailure
def test_func1(self):
assert 0
@unittest.expectedFailure
def test_func2(self):
assert 1
""")
result = testdir.runpytest("-rxX")
result.stdout.fnmatch_lines([
"*XFAIL*MyTestCase*test_func1*",
"*XPASS*MyTestCase*test_func2*",
"*1 xfailed*1 xpass*",
])


0 comments on commit 58933aa

Please sign in to comment.