From 0e8cd9297aa10d14ac3f65d8f99bbdab90196daa Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sun, 19 Aug 2012 13:45:26 +0200 Subject: [PATCH] fix issue 176: raises(AssertionError) now catches builtin AssertionError as well --- CHANGELOG | 4 ++++ _pytest/python.py | 5 +++++ testing/test_python.py | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 1ddc67002..78c7e4f25 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -44,6 +44,10 @@ Changes between 2.2.4 and 2.3.0.dev - fix issue 178: xml binary escapes are now wrapped in py.xml.raw +- fix issue 176: correctly catch the builtin AssertionError + even when we replaced AssertionError with a subclass on the + python level + - factory discovery no longer fails with magic global callables that provide no sane __code__ object (mock.call for example) diff --git a/_pytest/python.py b/_pytest/python.py index e03240089..7dd356001 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -777,6 +777,11 @@ def raises(ExpectedException, *args, **kwargs): """ __tracebackhide__ = True + if ExpectedException is AssertionError: + # we want to catch a AssertionError + # replace our subclass with the builtin one + # see https://bitbucket.org/hpk42/pytest/issue/176/pytestraises + from exceptions import AssertionError as ExpectedException if not args: return RaisesContext(ExpectedException) diff --git a/testing/test_python.py b/testing/test_python.py index e2fe18317..c3cd5dfb1 100644 --- a/testing/test_python.py +++ b/testing/test_python.py @@ -1419,6 +1419,14 @@ class TestRaises: except pytest.raises.Exception: pass + def test_raises_flip_builtin_AssertionError(self): + # we replace AssertionError on python level + # however c code might still raise the builtin one + import exceptions + pytest.raises(AssertionError,""" + raise exceptions.AssertionError + """) + @pytest.mark.skipif('sys.version < "2.5"') def test_raises_as_contextmanager(self, testdir): testdir.makepyfile("""