~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
from bzrlib.version import _get_bzr_source_tree
78
78
 
79
79
 
 
80
def _test_ids(test_suite):
 
81
    """Get the ids for the tests in a test suite."""
 
82
    return [t.id() for t in iter_suite_tests(test_suite)]
 
83
 
 
84
 
80
85
class SelftestTests(TestCase):
81
86
 
82
87
    def test_import_tests(self):
376
381
        formats = [(str, "C1", "C2"), (int, "D1", "D2")]
377
382
        adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
378
383
        self.assertEqual([
379
 
            ('str',
 
384
            ('str,str,str',
380
385
             {'interrepo_class': str,
381
386
              'repository_format': 'C1',
382
387
              'repository_format_to': 'C2',
383
388
              'transport_readonly_server': 'b',
384
389
              'transport_server': 'a'}),
385
 
            ('int',
 
390
            ('int,str,str',
386
391
             {'interrepo_class': int,
387
392
              'repository_format': 'D1',
388
393
              'repository_format_to': 'D2',
916
921
        test.run(result)
917
922
        self.assertEqual(
918
923
            [
919
 
            ('update', '[2 in 0s, 3 known failures] passing_test', None, None),
 
924
            ('update', '[2 in 0s] passing_test', None, None),
920
925
            ],
921
926
            pb.calls[2:])
922
927
 
992
997
        test.run(result)
993
998
        self.assertEqual(
994
999
            [
995
 
            ('update', '[2 in 0s, 2 missing features] passing_test', None, None),
 
1000
            ('update', '[2 in 0s, 2 missing] passing_test', None, None),
996
1001
            ],
997
1002
            pb.calls[1:])
998
1003
    
1120
1125
        self.assertTrue(result.wasSuccessful())
1121
1126
 
1122
1127
    def test_skipped_from_setup(self):
 
1128
        calls = []
1123
1129
        class SkippedSetupTest(TestCase):
1124
1130
 
1125
1131
            def setUp(self):
1126
 
                self.counter = 1
 
1132
                calls.append('setUp')
1127
1133
                self.addCleanup(self.cleanup)
1128
1134
                raise TestSkipped('skipped setup')
1129
1135
 
1131
1137
                self.fail('test reached')
1132
1138
 
1133
1139
            def cleanup(self):
1134
 
                self.counter -= 1
 
1140
                calls.append('cleanup')
1135
1141
 
1136
1142
        runner = TextTestRunner(stream=self._log_file)
1137
1143
        test = SkippedSetupTest('test_skip')
1138
1144
        result = self.run_test_runner(runner, test)
1139
1145
        self.assertTrue(result.wasSuccessful())
1140
1146
        # Check if cleanup was called the right number of times.
1141
 
        self.assertEqual(0, test.counter)
 
1147
        self.assertEqual(['setUp', 'cleanup'], calls)
1142
1148
 
1143
1149
    def test_skipped_from_test(self):
 
1150
        calls = []
1144
1151
        class SkippedTest(TestCase):
1145
1152
 
1146
1153
            def setUp(self):
1147
 
                self.counter = 1
 
1154
                calls.append('setUp')
1148
1155
                self.addCleanup(self.cleanup)
1149
1156
 
1150
1157
            def test_skip(self):
1151
1158
                raise TestSkipped('skipped test')
1152
1159
 
1153
1160
            def cleanup(self):
1154
 
                self.counter -= 1
 
1161
                calls.append('cleanup')
1155
1162
 
1156
1163
        runner = TextTestRunner(stream=self._log_file)
1157
1164
        test = SkippedTest('test_skip')
1158
1165
        result = self.run_test_runner(runner, test)
1159
1166
        self.assertTrue(result.wasSuccessful())
1160
1167
        # Check if cleanup was called the right number of times.
1161
 
        self.assertEqual(0, test.counter)
 
1168
        self.assertEqual(['setUp', 'cleanup'], calls)
1162
1169
 
1163
1170
    def test_not_applicable(self):
1164
1171
        # run a test that is skipped because it's not applicable
1721
1728
        self.loader = TestUtil.TestLoader()
1722
1729
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1723
1730
            'bzrlib.tests.test_selftest']))
1724
 
        self.all_names = self._test_ids(self.suite)
1725
 
 
1726
 
    def _test_ids(self, test_suite):
1727
 
        """Get the ids for the tests in a test suite."""
1728
 
        return [t.id() for t in iter_suite_tests(test_suite)]
 
1731
        self.all_names = _test_ids(self.suite)
1729
1732
 
1730
1733
    def test_condition_id_re(self):
1731
1734
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1732
1735
            'test_condition_id_re')
1733
1736
        filtered_suite = filter_suite_by_condition(self.suite,
1734
1737
            condition_id_re('test_condition_id_re'))
1735
 
        self.assertEqual([test_name], self._test_ids(filtered_suite))
 
1738
        self.assertEqual([test_name], _test_ids(filtered_suite))
1736
1739
 
1737
1740
    def test_condition_id_in_list(self):
1738
1741
        test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
1742
1745
            self.suite, tests.condition_id_in_list(id_list))
1743
1746
        my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
1744
1747
        re_filtered = filter_suite_by_re(self.suite, my_pattern)
1745
 
        self.assertEqual(self._test_ids(re_filtered),
1746
 
                         self._test_ids(filtered_suite))
 
1748
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1747
1749
 
1748
1750
    def test_condition_isinstance(self):
1749
1751
        filtered_suite = filter_suite_by_condition(self.suite,
1750
1752
            condition_isinstance(self.__class__))
1751
1753
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1752
1754
        re_filtered = filter_suite_by_re(self.suite, class_pattern)
1753
 
        self.assertEqual(self._test_ids(re_filtered),
1754
 
            self._test_ids(filtered_suite))
 
1755
        self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1755
1756
 
1756
1757
    def test_exclude_tests_by_condition(self):
1757
1758
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1760
1761
            lambda x:x.id() == excluded_name)
1761
1762
        self.assertEqual(len(self.all_names) - 1,
1762
1763
            filtered_suite.countTestCases())
1763
 
        self.assertFalse(excluded_name in self._test_ids(filtered_suite))
 
1764
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
1764
1765
        remaining_names = list(self.all_names)
1765
1766
        remaining_names.remove(excluded_name)
1766
 
        self.assertEqual(remaining_names, self._test_ids(filtered_suite))
 
1767
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
1767
1768
 
1768
1769
    def test_exclude_tests_by_re(self):
1769
 
        self.all_names = self._test_ids(self.suite)
 
1770
        self.all_names = _test_ids(self.suite)
1770
1771
        filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
1771
1772
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1772
1773
            'test_exclude_tests_by_re')
1773
1774
        self.assertEqual(len(self.all_names) - 1,
1774
1775
            filtered_suite.countTestCases())
1775
 
        self.assertFalse(excluded_name in self._test_ids(filtered_suite))
 
1776
        self.assertFalse(excluded_name in _test_ids(filtered_suite))
1776
1777
        remaining_names = list(self.all_names)
1777
1778
        remaining_names.remove(excluded_name)
1778
 
        self.assertEqual(remaining_names, self._test_ids(filtered_suite))
 
1779
        self.assertEqual(remaining_names, _test_ids(filtered_suite))
1779
1780
 
1780
1781
    def test_filter_suite_by_condition(self):
1781
1782
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1782
1783
            'test_filter_suite_by_condition')
1783
1784
        filtered_suite = filter_suite_by_condition(self.suite,
1784
1785
            lambda x:x.id() == test_name)
1785
 
        self.assertEqual([test_name], self._test_ids(filtered_suite))
 
1786
        self.assertEqual([test_name], _test_ids(filtered_suite))
1786
1787
 
1787
1788
    def test_filter_suite_by_re(self):
1788
1789
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
1789
 
        filtered_names = self._test_ids(filtered_suite)
 
1790
        filtered_names = _test_ids(filtered_suite)
1790
1791
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1791
1792
            'TestSelftestFiltering.test_filter_suite_by_re'])
1792
1793
 
1795
1796
                     'TestSelftestFiltering.test_filter_suite_by_id_list']
1796
1797
        filtered_suite = tests.filter_suite_by_id_list(
1797
1798
            self.suite, tests.TestIdList(test_list))
1798
 
        filtered_names = self._test_ids(filtered_suite)
 
1799
        filtered_names = _test_ids(filtered_suite)
1799
1800
        self.assertEqual(
1800
1801
            filtered_names,
1801
1802
            ['bzrlib.tests.test_selftest.'
1809
1810
    def test_randomize_suite(self):
1810
1811
        randomized_suite = randomize_suite(self.suite)
1811
1812
        # randomizing should not add or remove test names.
1812
 
        self.assertEqual(set(self._test_ids(self.suite)),
1813
 
            set(self._test_ids(randomized_suite)))
 
1813
        self.assertEqual(set(_test_ids(self.suite)),
 
1814
                         set(_test_ids(randomized_suite)))
1814
1815
        # Technically, this *can* fail, because random.shuffle(list) can be
1815
1816
        # equal to list. Trying multiple times just pushes the frequency back.
1816
1817
        # As its len(self.all_names)!:1, the failure frequency should be low
1817
1818
        # enough to ignore. RBC 20071021.
1818
1819
        # It should change the order.
1819
 
        self.assertNotEqual(self.all_names, self._test_ids(randomized_suite))
 
1820
        self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
1820
1821
        # But not the length. (Possibly redundant with the set test, but not
1821
1822
        # necessarily.)
1822
 
        self.assertEqual(len(self.all_names),
1823
 
            len(self._test_ids(randomized_suite)))
 
1823
        self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
1824
1824
 
1825
1825
    def test_sort_suite_by_re(self):
1826
1826
        sorted_suite = self.applyDeprecated(one_zero,
1827
1827
            sort_suite_by_re, self.suite, 'test_filter_suite_by_r')
1828
 
        sorted_names = self._test_ids(sorted_suite)
 
1828
        sorted_names = _test_ids(sorted_suite)
1829
1829
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1830
1830
            'TestSelftestFiltering.test_filter_suite_by_re')
1831
1831
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1832
1832
 
1833
1833
    def test_split_suit_by_re(self):
1834
 
        self.all_names = self._test_ids(self.suite)
 
1834
        self.all_names = _test_ids(self.suite)
1835
1835
        split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
1836
1836
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1837
1837
            'test_filter_suite_by_re')
1838
 
        self.assertEqual([filtered_name], self._test_ids(split_suite[0]))
1839
 
        self.assertFalse(filtered_name in self._test_ids(split_suite[1]))
 
1838
        self.assertEqual([filtered_name], _test_ids(split_suite[0]))
 
1839
        self.assertFalse(filtered_name in _test_ids(split_suite[1]))
1840
1840
        remaining_names = list(self.all_names)
1841
1841
        remaining_names.remove(filtered_name)
1842
 
        self.assertEqual(remaining_names, self._test_ids(split_suite[1]))
 
1842
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1843
1843
 
1844
1844
 
1845
1845
class TestCheckInventoryShape(TestCaseWithTransport):
1916
1916
        module.__class__.load_tests = load_tests
1917
1917
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
1918
1918
 
 
1919
    def test_load_tests_from_module_name_smoke_test(self):
 
1920
        loader = TestUtil.TestLoader()
 
1921
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
1922
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
 
1923
                          _test_ids(suite))
 
1924
 
 
1925
    def test_load_tests_from_module_name_with_bogus_module_name(self):
 
1926
        loader = TestUtil.TestLoader()
 
1927
        self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
 
1928
 
1919
1929
 
1920
1930
class TestTestIdList(tests.TestCase):
1921
1931
 
1954
1964
             'mod1.submod1',
1955
1965
             'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
1956
1966
             ])
1957
 
        self.assertTrue(id_list.is_module_name_used('mod1'))
1958
 
        self.assertTrue(id_list.is_module_name_used('mod1.submod1'))
1959
 
        self.assertTrue(id_list.is_module_name_used('mod1.submod2'))
1960
 
        self.assertTrue(id_list.test_in('mod1.cl1.meth1'))
1961
 
        self.assertTrue(id_list.test_in('mod1.submod1'))
1962
 
        self.assertTrue(id_list.test_in('mod1.func1'))
 
1967
        self.assertTrue(id_list.refers_to('mod1'))
 
1968
        self.assertTrue(id_list.refers_to('mod1.submod1'))
 
1969
        self.assertTrue(id_list.refers_to('mod1.submod2'))
 
1970
        self.assertTrue(id_list.includes('mod1.cl1.meth1'))
 
1971
        self.assertTrue(id_list.includes('mod1.submod1'))
 
1972
        self.assertTrue(id_list.includes('mod1.func1'))
1963
1973
 
1964
1974
    def test_bad_chars_in_params(self):
1965
1975
        id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
1966
 
        self.assertTrue(id_list.is_module_name_used('mod1'))
1967
 
        self.assertTrue(id_list.test_in('mod1.cl1.meth1(xx.yy)'))
 
1976
        self.assertTrue(id_list.refers_to('mod1'))
 
1977
        self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))
1968
1978
 
1969
1979
    def test_module_used(self):
1970
1980
        id_list = self._create_id_list(['mod.class.meth'])
1971
 
        self.assertTrue(id_list.is_module_name_used('mod'))
1972
 
        self.assertTrue(id_list.is_module_name_used('mod.class'))
1973
 
        self.assertTrue(id_list.is_module_name_used('mod.class.meth'))
 
1981
        self.assertTrue(id_list.refers_to('mod'))
 
1982
        self.assertTrue(id_list.refers_to('mod.class'))
 
1983
        self.assertTrue(id_list.refers_to('mod.class.meth'))
1974
1984
 
1975
1985
    def test_test_suite(self):
1976
1986
        # This test is slow, so we do a single test with one test in each
1989
1999
            # --no-plugins
1990
2000
            ]
1991
2001
        suite = tests.test_suite(test_list)
1992
 
        self.assertEquals(test_list, self._test_ids(suite))
 
2002
        self.assertEquals(test_list, _test_ids(suite))
 
2003
 
 
2004
    def test_test_suite_matches_id_list_with_unknown(self):
 
2005
        loader = TestUtil.TestLoader()
 
2006
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2007
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
 
2008
                     'bogus']
 
2009
        not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
 
2010
        self.assertEquals(['bogus'], not_found)
 
2011
        self.assertEquals([], duplicates)
 
2012
 
 
2013
    def test_suite_matches_id_list_with_duplicates(self):
 
2014
        loader = TestUtil.TestLoader()
 
2015
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2016
        dupes = loader.suiteClass()
 
2017
        for test in iter_suite_tests(suite):
 
2018
            dupes.addTest(test)
 
2019
            dupes.addTest(test) # Add it again
 
2020
 
 
2021
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
 
2022
        not_found, duplicates = tests.suite_matches_id_list(
 
2023
            dupes, test_list)
 
2024
        self.assertEquals([], not_found)
 
2025
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
 
2026
                          duplicates)
1993
2027
 
1994
2028
 
1995
2029
class TestLoadTestIdList(tests.TestCaseInTempDir):
2025
2059
        self.assertEquals('bar baz', tlist[3])
2026
2060
 
2027
2061
 
 
2062
class TestFilteredByModuleTestLoader(tests.TestCase):
 
2063
 
 
2064
    def _create_loader(self, test_list):
 
2065
        id_filter = tests.TestIdList(test_list)
 
2066
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
 
2067
        return loader
 
2068
 
 
2069
    def test_load_tests(self):
 
2070
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
 
2071
        loader = self._create_loader(test_list)
 
2072
 
 
2073
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2074
        self.assertEquals(test_list, _test_ids(suite))
 
2075
 
 
2076
    def test_exclude_tests(self):
 
2077
        test_list = ['bogus']
 
2078
        loader = self._create_loader(test_list)
 
2079
 
 
2080
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
 
2081
        self.assertEquals([], _test_ids(suite))