summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2010-06-20 19:49:46 -0300
committerGustavo Niemeyer <gustavo@niemeyer.net>2010-06-20 19:49:46 -0300
commit89bcdecfe4e13112a793afc261274bac375c8638 (patch)
tree4f09546a877eab661b30d092082a4b7efc12942f
parent3a4c487d0d08f51dca83e31f35aae5ecb3affb82 (diff)
parenta94098f9400c9b67b5d281dc90bdc51569a7a79a (diff)
downloadmocker-89bcdecfe4e13112a793afc261274bac375c8638.tar.gz
MockerTestCase.assertRaises() will now return the exception raised,
allowing further inspection of the raised exception (implemented by Thomas Hervé) (#299930).
-rw-r--r--NEWS4
-rw-r--r--mocker.py21
-rwxr-xr-xtest.py37
3 files changed, 62 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 2b8fca3..f38dd77 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,10 @@
- Unwrap bound methods on replace() and proxy(), as suggested
by James Henstridge (#270782).
+- MockerTestCase.assertRaises() will now return the exception raised,
+ allowing further inspection of the raised exception (implemented by
+ Thomas Hervé) (#299930).
+
- Fixed support for Python 2.6. Mocking of iterators was broken in
certain cases because, even though that's *not* documented, Python
tries to use __length_hint__ in some cases.
diff --git a/mocker.py b/mocker.py
index ae7970c..4e6cdd5 100644
--- a/mocker.py
+++ b/mocker.py
@@ -347,6 +347,26 @@ class MockerTestCase(unittest.TestCase):
(first.__name__, name, first_formatted,
second.__name__, name, second_formatted))
+ def failUnlessRaises(self, excClass, callableObj, *args, **kwargs):
+ """
+ Fail unless an exception of class excClass is thrown by callableObj
+ when invoked with arguments args and keyword arguments kwargs. If a
+ different type of exception is thrown, it will not be caught, and the
+ test case will be deemed to have suffered an error, exactly as for an
+ unexpected exception. It returns the exception instance if it matches
+ the given exception class.
+ """
+ try:
+ result = callableObj(*args, **kwargs)
+ except excClass, e:
+ return e
+ else:
+ excName = excClass
+ if hasattr(excClass, "__name__"):
+ excName = excClass.__name__
+ raise self.failureException(
+ "%s not raised (%r returned)" % (excName, result))
+
assertIs = failUnlessIs
assertIsNot = failIfIs
@@ -359,6 +379,7 @@ class MockerTestCase(unittest.TestCase):
assertApproximates = failUnlessApproximates
assertNotApproximates = failIfApproximates
assertMethodsMatch = failUnlessMethodsMatch
+ assertRaises = failUnlessRaises
# The following are missing in Python < 2.4.
assertTrue = unittest.TestCase.failUnless
diff --git a/test.py b/test.py
index 864bf7a..827cbea 100755
--- a/test.py
+++ b/test.py
@@ -824,6 +824,40 @@ class MockerTestCaseTest(TestCase):
except AssertionError:
self.fail("AssertionError shouldn't be raised")
+ def test_fail_unless_raises_succeeds(self):
+ class MyException(Exception):
+ pass
+ def f(*args):
+ raise MyException(*args)
+ error = self.test.failUnlessRaises(MyException, f, 1, "foo")
+ self.assertEquals(error.args, (1, "foo"))
+
+ def test_fail_unless_raises_error(self):
+ def f(*args):
+ return args
+ try:
+ self.test.failUnlessRaises(ValueError, f, 1, "foo")
+ except AssertionError, e:
+ self.assertEquals(
+ str(e),
+ "ValueError not raised ((1, 'foo') returned)")
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_fail_unless_raises_other_exception(self):
+ class MyException1(Exception):
+ pass
+ class MyException2(Exception):
+ pass
+ def f(*args):
+ raise MyException2(*args)
+ try:
+ self.test.failUnlessRaises(MyException1, f, 1, "foo")
+ except MyException2:
+ pass
+ else:
+ self.fail("MyException2 not raised")
+
def test_aliases(self):
get_method = MockerTestCase.__dict__.get
@@ -860,6 +894,9 @@ class MockerTestCaseTest(TestCase):
self.assertEquals(get_method("assertMethodsMatch"),
get_method("failUnlessMethodsMatch"))
+ self.assertEquals(get_method("assertRaises"),
+ get_method("failUnlessRaises"))
+
def test_twisted_trial_aliases(self):
get_method = MockerTestCase.__dict__.get