Merge pull request #34 from djipko/fix_assertraises_metaclasses
Fix assertRaises when the exception has a metaclass
This commit is contained in:
1
LICENSE
1
LICENSE
@@ -17,6 +17,7 @@ The testtools authors are:
|
|||||||
* Gavin Panella
|
* Gavin Panella
|
||||||
* Martin Pool
|
* Martin Pool
|
||||||
* Vincent Ladeuil
|
* Vincent Ladeuil
|
||||||
|
* Nikola Đipanov
|
||||||
|
|
||||||
and are collectively referred to as "testtools developers".
|
and are collectively referred to as "testtools developers".
|
||||||
|
|
||||||
|
|||||||
2
NEWS
2
NEWS
@@ -13,6 +13,8 @@ experimental and we might need to break it.
|
|||||||
|
|
||||||
Improvements
|
Improvements
|
||||||
------------
|
------------
|
||||||
|
* ``assertRaises`` works properly for exception classes that have custom
|
||||||
|
metaclasses
|
||||||
|
|
||||||
* ``ConcurrentTestSuite`` was silently eating exceptions that propagate from
|
* ``ConcurrentTestSuite`` was silently eating exceptions that propagate from
|
||||||
the test.run(result) method call. Ignoring them is fine in a normal test
|
the test.run(result) method call. Ignoring them is fine in a normal test
|
||||||
|
|||||||
@@ -44,7 +44,9 @@ class MatchesException(Matcher):
|
|||||||
if istext(value_re):
|
if istext(value_re):
|
||||||
value_re = AfterPreproccessing(str, MatchesRegex(value_re), False)
|
value_re = AfterPreproccessing(str, MatchesRegex(value_re), False)
|
||||||
self.value_re = value_re
|
self.value_re = value_re
|
||||||
self._is_instance = type(self.expected) not in classtypes() + (tuple,)
|
expected_type = type(self.expected)
|
||||||
|
self._is_instance = not any(issubclass(expected_type, class_type)
|
||||||
|
for class_type in classtypes() + (tuple,))
|
||||||
|
|
||||||
def match(self, other):
|
def match(self, other):
|
||||||
if type(other) != tuple:
|
if type(other) != tuple:
|
||||||
|
|||||||
@@ -302,6 +302,19 @@ class TestAssertions(TestCase):
|
|||||||
# assertRaises asserts that a callable raises a particular exception.
|
# assertRaises asserts that a callable raises a particular exception.
|
||||||
self.assertRaises(RuntimeError, self.raiseError, RuntimeError)
|
self.assertRaises(RuntimeError, self.raiseError, RuntimeError)
|
||||||
|
|
||||||
|
def test_assertRaises_exception_w_metaclass(self):
|
||||||
|
# assertRaises works when called for exceptions with custom metaclasses
|
||||||
|
class MyExMeta(type):
|
||||||
|
def __init__(cls, name, bases, dct):
|
||||||
|
""" Do some dummy metaclass stuff """
|
||||||
|
dct.update({'answer': 42})
|
||||||
|
type.__init__(cls, name, bases, dct)
|
||||||
|
|
||||||
|
class MyEx(Exception):
|
||||||
|
__metaclass__ = MyExMeta
|
||||||
|
|
||||||
|
self.assertRaises(MyEx, self.raiseError, MyEx)
|
||||||
|
|
||||||
def test_assertRaises_fails_when_no_error_raised(self):
|
def test_assertRaises_fails_when_no_error_raised(self):
|
||||||
# assertRaises raises self.failureException when it's passed a
|
# assertRaises raises self.failureException when it's passed a
|
||||||
# callable that raises no error.
|
# callable that raises no error.
|
||||||
|
|||||||
Reference in New Issue
Block a user