isinstance() on exception value instead of comparing types, consolidate tests

--HG--
branch : xfail-cause
This commit is contained in:
david@mcbf.net 2014-07-26 17:46:50 +02:00
parent 7b273b8577
commit 6a4492a22d
2 changed files with 17 additions and 45 deletions

View File

@ -62,14 +62,11 @@ class MarkEvaluator:
def wasvalid(self):
return not hasattr(self, 'exc')
def invalidraise(self, exctype):
def invalidraise(self, exc):
raises = self.get('raises')
if not raises:
return
if isinstance(raises, tuple):
return exctype not in raises
else:
return raises != exctype
return not isinstance(exc, raises)
def istrue(self):
try:
@ -182,7 +179,7 @@ def pytest_runtest_makereport(__multicall__, item, call):
if not item.config.option.runxfail:
if evalxfail.wasvalid() and evalxfail.istrue():
if call.excinfo:
if evalxfail.invalidraise(call.excinfo.type):
if evalxfail.invalidraise(call.excinfo.value):
rep.outcome = "failed"
return rep
else:

View File

@ -330,52 +330,27 @@ class TestXFail:
"*1 xfailed*",
])
def test_xfail_raises_match(self, testdir):
@pytest.mark.parametrize('params', [('TypeError', 'TypeError', "*1 xfailed*"),
('(AttributeError, TypeError)', 'TypeError',
"*1 xfailed*"),
('TypeError', 'IndexError', "*1 failed*"),
('(AttributeError, TypeError)', 'IndexError',
"*1 failed*"),
])
def test_xfail_raises(self, params, testdir):
expected, actual, matchline = params
p = testdir.makepyfile("""
import pytest
@pytest.mark.xfail(raises=TypeError)
@pytest.mark.xfail(raises=%s)
def test_raises():
raise TypeError()
""")
raise %s()
""" % (expected, actual))
result = testdir.runpytest(p)
result.stdout.fnmatch_lines([
"*1 xfailed*",
matchline,
])
def test_xfail_raises_mismatch(self, testdir):
p = testdir.makepyfile("""
import pytest
@pytest.mark.xfail(raises=IndexError)
def test_raises():
raise TypeError()
""")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines([
"*1 failed*",
])
def test_xfail_raises_tuple_match(self, testdir):
p = testdir.makepyfile("""
import pytest
@pytest.mark.xfail(raises=(AttributeError, TypeError))
def test_raises():
raise TypeError()
""")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines([
"*1 xfailed*",
])
def test_xfail_raises_tuple_mismatch(self, testdir):
p = testdir.makepyfile("""
import pytest
@pytest.mark.xfail(raises=(AttributeError, IndexError))
def test_raises():
raise TypeError()
""")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines([
"*1 failed*",
])
class TestXFailwithSetupTeardown:
def test_failing_setup_issue9(self, testdir):