868
868
excName = str(excClass)
869
869
raise self.failureException, "%s not raised" % excName
871
def assertRaises(self, excClass, func, *args, **kwargs):
872
"""Assert that a callable raises a particular exception.
874
:param excClass: As for the except statement, this may be either an
875
exception class, or a tuple of classes.
877
Returns the exception so that you can examine it.
880
func(*args, **kwargs)
884
if getattr(excClass,'__name__', None) is not None:
885
excName = excClass.__name__
888
excName = str(excClass)
889
raise self.failureException, "%s not raised" % excName
871
891
def assertIs(self, left, right, message=None):
872
892
if not (left is right):
873
893
if message is not None:
900
920
self.fail("%r is an instance of %s rather than %s" % (
901
921
obj, obj.__class__, kls))
923
def expectFailure(self, reason, assertion, *args, **kwargs):
924
"""Invoke a test, expecting it to fail for the given reason.
926
This is for assertions that ought to succeed, but currently fail.
927
(The failure is *expected* but not *wanted*.) Please be very precise
928
about the failure you're expecting. If a new bug is introduced,
929
AssertionError should be raised, not KnownFailure.
931
Frequently, expectFailure should be followed by an opposite assertion.
934
Intended to be used with a callable that raises AssertionError as the
935
'assertion' parameter. args and kwargs are passed to the 'assertion'.
937
Raises KnownFailure if the test fails. Raises AssertionError if the
942
self.expectFailure('Math is broken', self.assertNotEqual, 54,
944
self.assertEqual(42, dynamic_val)
946
This means that a dynamic_val of 54 will cause the test to raise
947
a KnownFailure. Once math is fixed and the expectFailure is removed,
948
only a dynamic_val of 42 will allow the test to pass. Anything other
949
than 54 or 42 will cause an AssertionError.
952
assertion(*args, **kwargs)
953
except AssertionError:
954
raise KnownFailure(reason)
956
self.fail('Unexpected success. Should have failed: %s' % reason)
903
958
def _capture_warnings(self, a_callable, *args, **kwargs):
904
959
"""A helper for callDeprecated and applyDeprecated.