/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Andrew Bennetts
  • Date: 2007-04-11 06:40:05 UTC
  • mfrom: (2404 +trunk)
  • mto: (2018.5.146 hpss)
  • mto: This revision was merged to the branch mainline in revision 2414.
  • Revision ID: andrew.bennetts@canonical.com-20070411064005-zylli6el5cz7kwnb
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,
 
761
            }
759
762
        self.addCleanup(self._restoreHooks)
760
763
        # this list of hooks must be kept in sync with the defaults
761
764
        # in branch.py
868
871
                excName = str(excClass)
869
872
            raise self.failureException, "%s not raised" % excName
870
873
 
 
874
    def assertRaises(self, excClass, func, *args, **kwargs):
 
875
        """Assert that a callable raises a particular exception.
 
876
 
 
877
        :param excClass: As for the except statement, this may be either an
 
878
        exception class, or a tuple of classes.
 
879
 
 
880
        Returns the exception so that you can examine it.
 
881
        """
 
882
        try:
 
883
            func(*args, **kwargs)
 
884
        except excClass, e:
 
885
            return e
 
886
        else:
 
887
            if getattr(excClass,'__name__', None) is not None:
 
888
                excName = excClass.__name__
 
889
            else:
 
890
                # probably a tuple
 
891
                excName = str(excClass)
 
892
            raise self.failureException, "%s not raised" % excName
 
893
 
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))
902
925
 
 
926
    def expectFailure(self, reason, assertion, *args, **kwargs):
 
927
        """Invoke a test, expecting it to fail for the given reason.
 
928
 
 
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.
 
933
 
 
934
        Frequently, expectFailure should be followed by an opposite assertion.
 
935
        See example below.
 
936
 
 
937
        Intended to be used with a callable that raises AssertionError as the
 
938
        'assertion' parameter.  args and kwargs are passed to the 'assertion'.
 
939
 
 
940
        Raises KnownFailure if the test fails.  Raises AssertionError if the
 
941
        test succeeds.
 
942
 
 
943
        example usage::
 
944
 
 
945
          self.expectFailure('Math is broken', self.assertNotEqual, 54,
 
946
                             dynamic_val)
 
947
          self.assertEqual(42, dynamic_val)
 
948
 
 
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.
 
953
        """
 
954
        try:
 
955
            assertion(*args, **kwargs)
 
956
        except AssertionError:
 
957
            raise KnownFailure(reason)
 
958
        else:
 
959
            self.fail('Unexpected success.  Should have failed: %s' % reason)
 
960
 
903
961
    def _capture_warnings(self, a_callable, *args, **kwargs):
904
962
        """A helper for callDeprecated and applyDeprecated.
905
963
 
1043
1101
            osutils.set_or_unset_env(name, value)
1044
1102
 
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)
1047
1106
 
1048
1107
    def knownFailure(self, reason):
1049
1108
        """This test has failed for some known reason."""