755
755
self._benchcalls = []
756
756
self._benchtime = None
757
757
# prevent hooks affecting tests
758
self._preserved_hooks = bzrlib.branch.Branch.hooks
758
self._preserved_hooks = {
759
bzrlib.branch.Branch:bzrlib.branch.Branch.hooks,
760
bzrlib.smart.server.SmartTCPServer:bzrlib.smart.server.SmartTCPServer.hooks,
759
762
self.addCleanup(self._restoreHooks)
760
763
# this list of hooks must be kept in sync with the defaults
868
871
excName = str(excClass)
869
872
raise self.failureException, "%s not raised" % excName
874
def assertRaises(self, excClass, func, *args, **kwargs):
875
"""Assert that a callable raises a particular exception.
877
:param excClass: As for the except statement, this may be either an
878
exception class, or a tuple of classes.
880
Returns the exception so that you can examine it.
883
func(*args, **kwargs)
887
if getattr(excClass,'__name__', None) is not None:
888
excName = excClass.__name__
891
excName = str(excClass)
892
raise self.failureException, "%s not raised" % excName
871
894
def assertIs(self, left, right, message=None):
872
895
if not (left is right):
873
896
if message is not None:
900
923
self.fail("%r is an instance of %s rather than %s" % (
901
924
obj, obj.__class__, kls))
926
def expectFailure(self, reason, assertion, *args, **kwargs):
927
"""Invoke a test, expecting it to fail for the given reason.
929
This is for assertions that ought to succeed, but currently fail.
930
(The failure is *expected* but not *wanted*.) Please be very precise
931
about the failure you're expecting. If a new bug is introduced,
932
AssertionError should be raised, not KnownFailure.
934
Frequently, expectFailure should be followed by an opposite assertion.
937
Intended to be used with a callable that raises AssertionError as the
938
'assertion' parameter. args and kwargs are passed to the 'assertion'.
940
Raises KnownFailure if the test fails. Raises AssertionError if the
945
self.expectFailure('Math is broken', self.assertNotEqual, 54,
947
self.assertEqual(42, dynamic_val)
949
This means that a dynamic_val of 54 will cause the test to raise
950
a KnownFailure. Once math is fixed and the expectFailure is removed,
951
only a dynamic_val of 42 will allow the test to pass. Anything other
952
than 54 or 42 will cause an AssertionError.
955
assertion(*args, **kwargs)
956
except AssertionError:
957
raise KnownFailure(reason)
959
self.fail('Unexpected success. Should have failed: %s' % reason)
903
961
def _capture_warnings(self, a_callable, *args, **kwargs):
904
962
"""A helper for callDeprecated and applyDeprecated.
1043
1101
osutils.set_or_unset_env(name, value)
1045
1103
def _restoreHooks(self):
1046
bzrlib.branch.Branch.hooks = self._preserved_hooks
1104
for klass, hooks in self._preserved_hooks.items():
1105
setattr(klass, 'hooks', hooks)
1048
1107
def knownFailure(self, reason):
1049
1108
"""This test has failed for some known reason."""