assertRaises(Exception, ...) considered harmful

Expecting that Exception is raised can end up passing an a test when an
unexpected error occurs. For instance, errors in the unit test itself
can be masked:

https://review.openstack.org/4848
https://review.openstack.org/4873
https://review.openstack.org/4874

Change a variety of unit tests to expect a more specific exception so
we don't run into false positive tests in the future.

Change-Id: Ibc0c63b1f6b5574a3ce93d9f02c9d1ff5ac4a8b0
This commit is contained in:
Johannes Erdfelt
2012-03-03 17:53:41 +00:00
parent d0cae6b5a1
commit 8813ab185d
11 changed files with 86 additions and 59 deletions

View File

@@ -60,7 +60,7 @@ def bad_function_error():
def bad_function_exception():
raise Exception()
raise test.TestingException()
class WrapExceptionTestCase(test.TestCase):
@@ -74,13 +74,15 @@ class WrapExceptionTestCase(test.TestCase):
def test_wrap_exception_throws_exception(self):
wrapped = exception.wrap_exception()
self.assertRaises(Exception, wrapped(bad_function_exception))
self.assertRaises(test.TestingException,
wrapped(bad_function_exception))
def test_wrap_exception_with_notifier(self):
notifier = FakeNotifier()
wrapped = exception.wrap_exception(notifier, "publisher", "event",
"level")
self.assertRaises(Exception, wrapped(bad_function_exception))
self.assertRaises(test.TestingException,
wrapped(bad_function_exception))
self.assertEquals(notifier.provided_publisher, "publisher")
self.assertEquals(notifier.provided_event, "event")
self.assertEquals(notifier.provided_priority, "level")
@@ -90,7 +92,8 @@ class WrapExceptionTestCase(test.TestCase):
def test_wrap_exception_with_notifier_defaults(self):
notifier = FakeNotifier()
wrapped = exception.wrap_exception(notifier)
self.assertRaises(Exception, wrapped(bad_function_exception))
self.assertRaises(test.TestingException,
wrapped(bad_function_exception))
self.assertEquals(notifier.provided_publisher, None)
self.assertEquals(notifier.provided_event, "bad_function_exception")
self.assertEquals(notifier.provided_priority, notifier.ERROR)