~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Vincent Ladeuil
  • Date: 2011-08-12 09:49:24 UTC
  • mfrom: (6015.9.10 2.4)
  • mto: This revision was merged to the branch mainline in revision 6066.
  • Revision ID: v.ladeuil+lp@free.fr-20110812094924-knc5s0g7vs31a2f1
Merge 2.4 into trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from testtools import (
30
30
    ExtendedToOriginalDecorator,
31
31
    MultiTestResult,
 
32
    __version__ as testtools_version,
32
33
    )
33
34
from testtools.content import Content
34
35
from testtools.content_type import ContentType
93
94
            DocTestMatches(u"...a test message\n", doctest.ELLIPSIS))
94
95
 
95
96
 
96
 
class TestUnicodeFilename(tests.TestCase):
97
 
 
98
 
    def test_probe_passes(self):
99
 
        """UnicodeFilename._probe passes."""
100
 
        # We can't test much more than that because the behaviour depends
101
 
        # on the platform.
102
 
        tests.UnicodeFilename._probe()
103
 
 
104
 
 
105
97
class TestTreeShape(tests.TestCaseInTempDir):
106
98
 
107
99
    def test_unicode_paths(self):
108
 
        self.requireFeature(tests.UnicodeFilename)
 
100
        self.requireFeature(features.UnicodeFilenameFeature)
109
101
 
110
102
        filename = u'hell\u00d8'
111
103
        self.build_tree_contents([(filename, 'contents of hello')])
721
713
class TestProfileResult(tests.TestCase):
722
714
 
723
715
    def test_profiles_tests(self):
724
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
716
        self.requireFeature(features.lsprof_feature)
725
717
        terminal = testtools.testresult.doubles.ExtendedTestResult()
726
718
        result = tests.ProfileResult(terminal)
727
719
        class Sample(tests.TestCase):
782
774
 
783
775
    def test_lsprofiling(self):
784
776
        """Verbose test result prints lsprof statistics from test cases."""
785
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
777
        self.requireFeature(features.lsprof_feature)
786
778
        result_stream = StringIO()
787
779
        result = bzrlib.tests.VerboseTestResult(
788
780
            result_stream,
833
825
        self.assertEndsWith(sio.getvalue(), "OK    50002ms\n")
834
826
 
835
827
    def test_known_failure(self):
836
 
        """A KnownFailure being raised should trigger several result actions."""
 
828
        """Using knownFailure should trigger several result actions."""
837
829
        class InstrumentedTestResult(tests.ExtendedTestResult):
838
830
            def stopTestRun(self): pass
839
831
            def report_tests_starting(self): pass
842
834
        result = InstrumentedTestResult(None, None, None, None)
843
835
        class Test(tests.TestCase):
844
836
            def test_function(self):
845
 
                raise tests.KnownFailure('failed!')
 
837
                self.knownFailure('failed!')
846
838
        test = Test("test_function")
847
839
        test.run(result)
848
840
        # it should invoke 'report_known_failure'.
864
856
            descriptions=0,
865
857
            verbosity=2,
866
858
            )
867
 
        test = self.get_passing_test()
868
 
        result.startTest(test)
869
 
        prefix = len(result_stream.getvalue())
870
 
        # the err parameter has the shape:
871
 
        # (class, exception object, traceback)
872
 
        # KnownFailures dont get their tracebacks shown though, so we
873
 
        # can skip that.
874
 
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
875
 
        result.report_known_failure(test, err)
876
 
        output = result_stream.getvalue()[prefix:]
877
 
        lines = output.splitlines()
878
 
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
879
 
        if sys.version_info > (2, 7):
880
 
            self.expectFailure("_ExpectedFailure on 2.7 loses the message",
881
 
                self.assertNotEqual, lines[1], '    ')
882
 
        self.assertEqual(lines[1], '    foo')
883
 
        self.assertEqual(2, len(lines))
 
859
        _get_test("test_xfail").run(result)
 
860
        self.assertContainsRe(result_stream.getvalue(),
 
861
            "\n\\S+\\.test_xfail\\s+XFAIL\\s+\\d+ms\n"
 
862
            "\\s*(?:Text attachment: )?reason"
 
863
            "(?:\n-+\n|: {{{)"
 
864
            "this_fails"
 
865
            "(?:\n-+\n|}}}\n)")
884
866
 
885
867
    def get_passing_test(self):
886
868
        """Return a test object that can't be run usefully."""
897
879
                self._call = test, feature
898
880
        result = InstrumentedTestResult(None, None, None, None)
899
881
        test = SampleTestCase('_test_pass')
900
 
        feature = tests.Feature()
 
882
        feature = features.Feature()
901
883
        result.startTest(test)
902
884
        result.addNotSupported(test, feature)
903
885
        # it should invoke 'report_unsupported'.
922
904
            verbosity=2,
923
905
            )
924
906
        test = self.get_passing_test()
925
 
        feature = tests.Feature()
 
907
        feature = features.Feature()
926
908
        result.startTest(test)
927
909
        prefix = len(result_stream.getvalue())
928
910
        result.report_unsupported(test, feature)
941
923
            def addNotSupported(self, test, feature):
942
924
                self._call = test, feature
943
925
        result = InstrumentedTestResult(None, None, None, None)
944
 
        feature = tests.Feature()
 
926
        feature = features.Feature()
945
927
        class Test(tests.TestCase):
946
928
            def test_function(self):
947
929
                raise tests.UnavailableFeature(feature)
966
948
    def test_strict_with_known_failure(self):
967
949
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
968
950
                                             verbosity=1)
969
 
        test = self.get_passing_test()
970
 
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
971
 
        result.addExpectedFailure(test, err)
 
951
        test = _get_test("test_xfail")
 
952
        test.run(result)
972
953
        self.assertFalse(result.wasStrictlySuccessful())
973
954
        self.assertEqual(None, result._extractBenchmarkTime(test))
974
955
 
1006
987
        self.assertEquals(2, result.count)
1007
988
 
1008
989
 
1009
 
class TestUnicodeFilenameFeature(tests.TestCase):
1010
 
 
1011
 
    def test_probe_passes(self):
1012
 
        """UnicodeFilenameFeature._probe passes."""
1013
 
        # We can't test much more than that because the behaviour depends
1014
 
        # on the platform.
1015
 
        tests.UnicodeFilenameFeature._probe()
1016
 
 
1017
 
 
1018
990
class TestRunner(tests.TestCase):
1019
991
 
1020
992
    def dummy_test(self):
1090
1062
                self.expectFailure("No absolute truth", self.assertTrue, True)
1091
1063
        runner = tests.TextTestRunner(stream=StringIO())
1092
1064
        result = self.run_test_runner(runner, Test("test_truth"))
1093
 
        self.assertContainsRe(runner.stream.getvalue(),
1094
 
            "=+\n"
1095
 
            "FAIL: \\S+\.test_truth\n"
1096
 
            "-+\n"
1097
 
            "(?:.*\n)*"
1098
 
            "No absolute truth\n"
1099
 
            "(?:.*\n)*"
1100
 
            "-+\n"
1101
 
            "Ran 1 test in .*\n"
1102
 
            "\n"
1103
 
            "FAILED \\(failures=1\\)\n\\Z")
 
1065
        if testtools_version <= (0, 9, 11):
 
1066
            self.assertContainsRe(runner.stream.getvalue(),
 
1067
                "=+\n"
 
1068
                "FAIL: \\S+\.test_truth\n"
 
1069
                "-+\n"
 
1070
                "(?:.*\n)*"
 
1071
                "No absolute truth\n"
 
1072
                "(?:.*\n)*"
 
1073
                "-+\n"
 
1074
                "Ran 1 test in .*\n"
 
1075
                "\n"
 
1076
                "FAILED \\(failures=1\\)\n\\Z")
 
1077
        else:
 
1078
            self.assertContainsRe(runner.stream.getvalue(),
 
1079
                "=+\n"
 
1080
                "FAIL: \\S+\.test_truth\n"
 
1081
                "-+\n"
 
1082
                "Empty attachments:\n"
 
1083
                "  log\n"
 
1084
                "\n"
 
1085
                "reason: {{{No absolute truth}}}\n"
 
1086
                "-+\n"
 
1087
                "Ran 1 test in .*\n"
 
1088
                "\n"
 
1089
                "FAILED \\(failures=1\\)\n\\Z")
1104
1090
 
1105
1091
    def test_result_decorator(self):
1106
1092
        # decorate results
1191
1177
 
1192
1178
    def test_unsupported_features_listed(self):
1193
1179
        """When unsupported features are encountered they are detailed."""
1194
 
        class Feature1(tests.Feature):
 
1180
        class Feature1(features.Feature):
1195
1181
            def _probe(self): return False
1196
 
        class Feature2(tests.Feature):
 
1182
        class Feature2(features.Feature):
1197
1183
            def _probe(self): return False
1198
1184
        # create sample tests
1199
1185
        test1 = SampleTestCase('_test_pass')
1265
1251
            lambda trace=False: "ascii")
1266
1252
        result = self.run_test_runner(tests.TextTestRunner(stream=out),
1267
1253
            FailureWithUnicode("test_log_unicode"))
1268
 
        self.assertContainsRe(out.getvalue(),
1269
 
            "Text attachment: log\n"
1270
 
            "-+\n"
1271
 
            "\d+\.\d+  \\\\u2606\n"
1272
 
            "-+\n")
 
1254
        if testtools_version > (0, 9, 11):
 
1255
            self.assertContainsRe(out.getvalue(), "log: {{{\d+\.\d+  \\\\u2606}}}")
 
1256
        else:
 
1257
            self.assertContainsRe(out.getvalue(),
 
1258
                "Text attachment: log\n"
 
1259
                "-+\n"
 
1260
                "\d+\.\d+  \\\\u2606\n"
 
1261
                "-+\n")
1273
1262
 
1274
1263
 
1275
1264
class SampleTestCase(tests.TestCase):
1476
1465
 
1477
1466
        Each self.time() call is individually and separately profiled.
1478
1467
        """
1479
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
1468
        self.requireFeature(features.lsprof_feature)
1480
1469
        # overrides the class member with an instance member so no cleanup
1481
1470
        # needed.
1482
1471
        self._gather_lsprof_in_benchmarks = True
1512
1501
 
1513
1502
    def test_requireFeature_available(self):
1514
1503
        """self.requireFeature(available) is a no-op."""
1515
 
        class Available(tests.Feature):
 
1504
        class Available(features.Feature):
1516
1505
            def _probe(self):return True
1517
1506
        feature = Available()
1518
1507
        self.requireFeature(feature)
1519
1508
 
1520
1509
    def test_requireFeature_unavailable(self):
1521
1510
        """self.requireFeature(unavailable) raises UnavailableFeature."""
1522
 
        class Unavailable(tests.Feature):
 
1511
        class Unavailable(features.Feature):
1523
1512
            def _probe(self):return False
1524
1513
        feature = Unavailable()
1525
1514
        self.assertRaises(tests.UnavailableFeature,
1697
1686
    return a + b
1698
1687
 
1699
1688
 
1700
 
class _MissingFeature(tests.Feature):
 
1689
class _MissingFeature(features.Feature):
1701
1690
    def _probe(self):
1702
1691
        return False
1703
1692
missing_feature = _MissingFeature()
1754
1743
        result = self._run_test('test_fail')
1755
1744
        self.assertEqual(1, len(result.failures))
1756
1745
        result_content = result.failures[0][1]
1757
 
        self.assertContainsRe(result_content, 'Text attachment: log')
 
1746
        if testtools_version < (0, 9, 12):
 
1747
            self.assertContainsRe(result_content, 'Text attachment: log')
1758
1748
        self.assertContainsRe(result_content, 'this was a failing test')
1759
1749
 
1760
1750
    def test_error_has_log(self):
1761
1751
        result = self._run_test('test_error')
1762
1752
        self.assertEqual(1, len(result.errors))
1763
1753
        result_content = result.errors[0][1]
1764
 
        self.assertContainsRe(result_content, 'Text attachment: log')
 
1754
        if testtools_version < (0, 9, 12):
 
1755
            self.assertContainsRe(result_content, 'Text attachment: log')
1765
1756
        self.assertContainsRe(result_content, 'this test errored')
1766
1757
 
1767
1758
    def test_skip_has_no_log(self):
2049
2040
        self.assertLength(2, output.readlines())
2050
2041
 
2051
2042
    def test_lsprof_tests(self):
2052
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
2043
        self.requireFeature(features.lsprof_feature)
2053
2044
        results = []
2054
2045
        class Test(object):
2055
2046
            def __call__(test, result):
2619
2610
        self.assertEqual('bzr: interrupted\n', result[1])
2620
2611
 
2621
2612
 
2622
 
class TestFeature(tests.TestCase):
2623
 
 
2624
 
    def test_caching(self):
2625
 
        """Feature._probe is called by the feature at most once."""
2626
 
        class InstrumentedFeature(tests.Feature):
2627
 
            def __init__(self):
2628
 
                super(InstrumentedFeature, self).__init__()
2629
 
                self.calls = []
2630
 
            def _probe(self):
2631
 
                self.calls.append('_probe')
2632
 
                return False
2633
 
        feature = InstrumentedFeature()
2634
 
        feature.available()
2635
 
        self.assertEqual(['_probe'], feature.calls)
2636
 
        feature.available()
2637
 
        self.assertEqual(['_probe'], feature.calls)
2638
 
 
2639
 
    def test_named_str(self):
2640
 
        """Feature.__str__ should thunk to feature_name()."""
2641
 
        class NamedFeature(tests.Feature):
2642
 
            def feature_name(self):
2643
 
                return 'symlinks'
2644
 
        feature = NamedFeature()
2645
 
        self.assertEqual('symlinks', str(feature))
2646
 
 
2647
 
    def test_default_str(self):
2648
 
        """Feature.__str__ should default to __class__.__name__."""
2649
 
        class NamedFeature(tests.Feature):
2650
 
            pass
2651
 
        feature = NamedFeature()
2652
 
        self.assertEqual('NamedFeature', str(feature))
2653
 
 
2654
 
 
2655
 
class TestUnavailableFeature(tests.TestCase):
2656
 
 
2657
 
    def test_access_feature(self):
2658
 
        feature = tests.Feature()
2659
 
        exception = tests.UnavailableFeature(feature)
2660
 
        self.assertIs(feature, exception.args[0])
2661
 
 
2662
 
 
2663
 
simple_thunk_feature = tests._CompatabilityThunkFeature(
2664
 
    deprecated_in((2, 1, 0)),
2665
 
    'bzrlib.tests.test_selftest',
2666
 
    'simple_thunk_feature','UnicodeFilename',
2667
 
    replacement_module='bzrlib.tests'
2668
 
    )
2669
 
 
2670
 
class Test_CompatibilityFeature(tests.TestCase):
2671
 
 
2672
 
    def test_does_thunk(self):
2673
 
        res = self.callDeprecated(
2674
 
            ['bzrlib.tests.test_selftest.simple_thunk_feature was deprecated'
2675
 
             ' in version 2.1.0. Use bzrlib.tests.UnicodeFilename instead.'],
2676
 
            simple_thunk_feature.available)
2677
 
        self.assertEqual(tests.UnicodeFilename.available(), res)
2678
 
 
2679
 
 
2680
 
class TestModuleAvailableFeature(tests.TestCase):
2681
 
 
2682
 
    def test_available_module(self):
2683
 
        feature = tests.ModuleAvailableFeature('bzrlib.tests')
2684
 
        self.assertEqual('bzrlib.tests', feature.module_name)
2685
 
        self.assertEqual('bzrlib.tests', str(feature))
2686
 
        self.assertTrue(feature.available())
2687
 
        self.assertIs(tests, feature.module)
2688
 
 
2689
 
    def test_unavailable_module(self):
2690
 
        feature = tests.ModuleAvailableFeature('bzrlib.no_such_module_exists')
2691
 
        self.assertEqual('bzrlib.no_such_module_exists', str(feature))
2692
 
        self.assertFalse(feature.available())
2693
 
        self.assertIs(None, feature.module)
2694
 
 
2695
 
 
2696
2613
class TestSelftestFiltering(tests.TestCase):
2697
2614
 
2698
2615
    def setUp(self):