From 6a4492a22dac45fd7e3cb1c7adc987328bfc662e Mon Sep 17 00:00:00 2001 From: "david@mcbf.net" Date: Sat, 26 Jul 2014 17:46:50 +0200 Subject: [PATCH] isinstance() on exception value instead of comparing types, consolidate tests --HG-- branch : xfail-cause --- _pytest/skipping.py | 9 +++---- testing/test_skipping.py | 53 +++++++++++----------------------------- 2 files changed, 17 insertions(+), 45 deletions(-) diff --git a/_pytest/skipping.py b/_pytest/skipping.py index e65ac577f..aee98ffa8 100644 --- a/_pytest/skipping.py +++ b/_pytest/skipping.py @@ -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: diff --git a/testing/test_skipping.py b/testing/test_skipping.py index fdf73efde..cc5c29c0e 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -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):