~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Andrew Bennetts
  • Date: 2007-08-30 08:11:54 UTC
  • mfrom: (2766 +trunk)
  • mto: (2535.3.55 repo-refactor)
  • mto: This revision was merged to the branch mainline in revision 2772.
  • Revision ID: andrew.bennetts@canonical.com-20070830081154-16hebp2xwr15x2hc
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    )
36
36
from bzrlib.progress import _BaseProgressBar
37
37
from bzrlib.repofmt import weaverepo
38
 
from bzrlib.symbol_versioning import zero_ten, zero_eleven
 
38
from bzrlib.symbol_versioning import (
 
39
        zero_ten,
 
40
        zero_eleven,
 
41
        )
39
42
from bzrlib.tests import (
40
43
                          ChrootedTestCase,
41
44
                          ExtendedTestResult,
45
48
                          TestCaseInTempDir,
46
49
                          TestCaseWithMemoryTransport,
47
50
                          TestCaseWithTransport,
 
51
                          TestNotApplicable,
48
52
                          TestSkipped,
49
53
                          TestSuite,
50
54
                          TestUtil,
703
707
 
704
708
class TestTestResult(TestCase):
705
709
 
706
 
    def test_elapsed_time_with_benchmarking(self):
 
710
    def check_timing(self, test_case, expected_re):
707
711
        result = bzrlib.tests.TextTestResult(self._log_file,
708
 
                                        descriptions=0,
709
 
                                        verbosity=1,
710
 
                                        )
711
 
        result._recordTestStartTime()
712
 
        time.sleep(0.003)
713
 
        result.extractBenchmarkTime(self)
714
 
        timed_string = result._testTimeString()
715
 
        # without explicit benchmarking, we should get a simple time.
716
 
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
 
712
                descriptions=0,
 
713
                verbosity=1,
 
714
                )
 
715
        test_case.run(result)
 
716
        timed_string = result._testTimeString(test_case)
 
717
        self.assertContainsRe(timed_string, expected_re)
 
718
 
 
719
    def test_test_reporting(self):
 
720
        class ShortDelayTestCase(TestCase):
 
721
            def test_short_delay(self):
 
722
                time.sleep(0.003)
 
723
            def test_short_benchmark(self):
 
724
                self.time(time.sleep, 0.003)
 
725
        self.check_timing(ShortDelayTestCase('test_short_delay'),
 
726
                          r"^ +[0-9]+ms$")
717
727
        # if a benchmark time is given, we want a x of y style result.
718
 
        self.time(time.sleep, 0.001)
719
 
        result.extractBenchmarkTime(self)
720
 
        timed_string = result._testTimeString()
721
 
        self.assertContainsRe(
722
 
            timed_string, "^ +[0-9]+ms/ +[0-9]+ms$")
723
 
        # extracting the time from a non-bzrlib testcase sets to None
724
 
        result._recordTestStartTime()
725
 
        result.extractBenchmarkTime(
726
 
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
727
 
        timed_string = result._testTimeString()
728
 
        self.assertContainsRe(timed_string, "^ +[0-9]+ms$")
729
 
        # cheat. Yes, wash thy mouth out with soap.
730
 
        self._benchtime = None
 
728
        self.check_timing(ShortDelayTestCase('test_short_benchmark'),
 
729
                          r"^ +[0-9]+ms/ +[0-9]+ms$")
731
730
 
 
731
    def test_unittest_reporting_unittest_class(self):
 
732
        # getting the time from a non-bzrlib test works ok
 
733
        class ShortDelayTestCase(unittest.TestCase):
 
734
            def test_short_delay(self):
 
735
                time.sleep(0.003)
 
736
        self.check_timing(ShortDelayTestCase('test_short_delay'),
 
737
                          r"^ +[0-9]+ms$")
 
738
        
732
739
    def test_assigned_benchmark_file_stores_date(self):
733
740
        output = StringIO()
734
741
        result = bzrlib.tests.TextTestResult(self._log_file,
737
744
                                        bench_history=output
738
745
                                        )
739
746
        output_string = output.getvalue()
740
 
        
741
747
        # if you are wondering about the regexp please read the comment in
742
748
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
743
749
        # XXX: what comment?  -- Andrew Bennetts
842
848
            )
843
849
        test = self.get_passing_test()
844
850
        result.startTest(test)
845
 
        result.extractBenchmarkTime(test)
846
851
        prefix = len(result_stream.getvalue())
847
852
        # the err parameter has the shape:
848
853
        # (class, exception object, traceback)
868
873
        test = self.get_passing_test()
869
874
        # this seeds the state to handle reporting the test.
870
875
        result.startTest(test)
871
 
        result.extractBenchmarkTime(test)
872
876
        # the err parameter has the shape:
873
877
        # (class, exception object, traceback)
874
878
        # KnownFailures dont get their tracebacks shown though, so we
933
937
        test = self.get_passing_test()
934
938
        feature = Feature()
935
939
        result.startTest(test)
936
 
        result.extractBenchmarkTime(test)
937
940
        prefix = len(result_stream.getvalue())
938
941
        result.report_unsupported(test, feature)
939
942
        output = result_stream.getvalue()[prefix:]
953
956
        feature = Feature()
954
957
        # this seeds the state to handle reporting the test.
955
958
        result.startTest(test)
956
 
        result.extractBenchmarkTime(test)
957
959
        result.report_unsupported(test, feature)
958
960
        # no output on unsupported features
959
961
        self.assertEqual(
992
994
 
993
995
    def test_strict_with_unsupported_feature(self):
994
996
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
995
 
                                                verbosity=1)
 
997
                                             verbosity=1)
996
998
        test = self.get_passing_test()
997
999
        feature = "Unsupported Feature"
998
1000
        result.addNotSupported(test, feature)
999
1001
        self.assertFalse(result.wasStrictlySuccessful())
1000
 
 
 
1002
        self.assertEqual(None, result._extractBenchmarkTime(test))
 
1003
 
1001
1004
    def test_strict_with_known_failure(self):
1002
1005
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1003
 
                                                verbosity=1)
 
1006
                                             verbosity=1)
1004
1007
        test = self.get_passing_test()
1005
1008
        err = (KnownFailure, KnownFailure('foo'), None)
1006
 
        result.addKnownFailure(test, err)
 
1009
        result._addKnownFailure(test, err)
1007
1010
        self.assertFalse(result.wasStrictlySuccessful())
 
1011
        self.assertEqual(None, result._extractBenchmarkTime(test))
1008
1012
 
1009
1013
    def test_strict_with_success(self):
1010
1014
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1011
 
                                                verbosity=1)
 
1015
                                             verbosity=1)
1012
1016
        test = self.get_passing_test()
1013
1017
        result.addSuccess(test)
1014
1018
        self.assertTrue(result.wasStrictlySuccessful())
 
1019
        self.assertEqual(None, result._extractBenchmarkTime(test))
1015
1020
 
1016
1021
 
1017
1022
class TestRunner(TestCase):
1131
1136
        # Check if cleanup was called the right number of times.
1132
1137
        self.assertEqual(0, test.counter)
1133
1138
 
 
1139
    def test_not_applicable(self):
 
1140
        # run a test that is skipped because it's not applicable
 
1141
        def not_applicable_test():
 
1142
            from bzrlib.tests import TestNotApplicable
 
1143
            raise TestNotApplicable('this test never runs')
 
1144
        out = StringIO()
 
1145
        runner = TextTestRunner(stream=out, verbosity=2)
 
1146
        test = unittest.FunctionTestCase(not_applicable_test)
 
1147
        result = self.run_test_runner(runner, test)
 
1148
        self._log_file.write(out.getvalue())
 
1149
        self.assertTrue(result.wasSuccessful())
 
1150
        self.assertTrue(result.wasStrictlySuccessful())
 
1151
        self.assertContainsRe(out.getvalue(),
 
1152
                r'(?m)not_applicable_test   * N/A')
 
1153
        self.assertContainsRe(out.getvalue(),
 
1154
                r'(?m)^    this test never runs')
 
1155
 
 
1156
    def test_not_applicable_demo(self):
 
1157
        # just so you can see it in the test output
 
1158
        raise TestNotApplicable('this test is just a demonstation')
 
1159
 
1134
1160
    def test_unsupported_features_listed(self):
1135
1161
        """When unsupported features are encountered they are detailed."""
1136
1162
        class Feature1(Feature):