~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-12-19 09:10:53 UTC
  • mfrom: (3128.2.2 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071219091053-81xgp971m1pgmccf
(robertc) Allow arbitrary parameteriZation of tests cleanly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
from bzrlib.progress import _BaseProgressBar
37
37
from bzrlib.repofmt import weaverepo
38
38
from bzrlib.symbol_versioning import (
39
 
        zero_ten,
40
 
        zero_eleven,
41
 
        )
 
39
    one_zero,
 
40
    zero_eleven,
 
41
    zero_ten,
 
42
    )
42
43
from bzrlib.tests import (
43
44
                          ChrootedTestCase,
44
45
                          ExtendedTestResult,
54
55
                          TestUtil,
55
56
                          TextTestRunner,
56
57
                          UnavailableFeature,
 
58
                          condition_id_re,
 
59
                          condition_isinstance,
 
60
                          exclude_tests_by_condition,
 
61
                          exclude_tests_by_re,
 
62
                          filter_suite_by_condition,
 
63
                          filter_suite_by_re,
57
64
                          iter_suite_tests,
58
 
                          filter_suite_by_re,
 
65
                          preserve_input,
 
66
                          randomize_suite,
59
67
                          sort_suite_by_re,
 
68
                          split_suite_by_re,
60
69
                          test_lsprof,
61
70
                          test_suite,
62
71
                          )
521
530
        # because each optimiser can be direction specific, we need to test
522
531
        # each optimiser in its chosen direction.
523
532
        # unlike the TestProviderAdapter we dont want to automatically add a
524
 
        # parameterised one for WorkingTree - the optimisers will tell us what
 
533
        # parameterized one for WorkingTree - the optimisers will tell us what
525
534
        # ones to add.
526
535
        from bzrlib.tests.tree_implementations import (
527
536
            return_parameter,
1660
1669
        self.loader = TestUtil.TestLoader()
1661
1670
        self.suite.addTest(self.loader.loadTestsFromModuleNames([
1662
1671
            'bzrlib.tests.test_selftest']))
1663
 
        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
 
1672
        self.all_names = self._test_ids(self.suite)
 
1673
 
 
1674
    def _test_ids(self, test_suite):
 
1675
        """Get the ids for the tests in a test suite."""
 
1676
        return [t.id() for t in iter_suite_tests(test_suite)]
 
1677
 
 
1678
    def test_condition_id_re(self):
 
1679
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1680
            'test_condition_id_re')
 
1681
        filtered_suite = filter_suite_by_condition(self.suite,
 
1682
            condition_id_re('test_condition_id_re'))
 
1683
        self.assertEqual([test_name], self._test_ids(filtered_suite))
 
1684
 
 
1685
    def test_condition_isinstance(self):
 
1686
        filtered_suite = filter_suite_by_condition(self.suite,
 
1687
            condition_isinstance(self.__class__))
 
1688
        class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1689
        re_filtered = filter_suite_by_re(self.suite, class_pattern)
 
1690
        self.assertEqual(self._test_ids(re_filtered),
 
1691
            self._test_ids(filtered_suite))
 
1692
 
 
1693
    def test_exclude_tests_by_condition(self):
 
1694
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1695
            'test_exclude_tests_by_condition')
 
1696
        filtered_suite = exclude_tests_by_condition(self.suite,
 
1697
            lambda x:x.id() == excluded_name)
 
1698
        self.assertEqual(len(self.all_names) - 1,
 
1699
            filtered_suite.countTestCases())
 
1700
        self.assertFalse(excluded_name in self._test_ids(filtered_suite))
 
1701
        remaining_names = list(self.all_names)
 
1702
        remaining_names.remove(excluded_name)
 
1703
        self.assertEqual(remaining_names, self._test_ids(filtered_suite))
 
1704
 
 
1705
    def test_exclude_tests_by_re(self):
 
1706
        self.all_names = self._test_ids(self.suite)
 
1707
        filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
 
1708
        excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1709
            'test_exclude_tests_by_re')
 
1710
        self.assertEqual(len(self.all_names) - 1,
 
1711
            filtered_suite.countTestCases())
 
1712
        self.assertFalse(excluded_name in self._test_ids(filtered_suite))
 
1713
        remaining_names = list(self.all_names)
 
1714
        remaining_names.remove(excluded_name)
 
1715
        self.assertEqual(remaining_names, self._test_ids(filtered_suite))
 
1716
 
 
1717
    def test_filter_suite_by_condition(self):
 
1718
        test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1719
            'test_filter_suite_by_condition')
 
1720
        filtered_suite = filter_suite_by_condition(self.suite,
 
1721
            lambda x:x.id() == test_name)
 
1722
        self.assertEqual([test_name], self._test_ids(filtered_suite))
1664
1723
 
1665
1724
    def test_filter_suite_by_re(self):
1666
 
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
1667
 
        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
 
1725
        filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
 
1726
        filtered_names = self._test_ids(filtered_suite)
1668
1727
        self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1669
1728
            'TestSelftestFiltering.test_filter_suite_by_re'])
1670
 
            
 
1729
 
 
1730
    def test_preserve_input(self):
 
1731
        # NB: Surely this is something in the stdlib to do this?
 
1732
        self.assertTrue(self.suite is preserve_input(self.suite))
 
1733
        self.assertTrue("@#$" is preserve_input("@#$"))
 
1734
 
 
1735
    def test_randomize_suite(self):
 
1736
        randomized_suite = randomize_suite(self.suite)
 
1737
        # randomizing should not add or remove test names.
 
1738
        self.assertEqual(set(self._test_ids(self.suite)),
 
1739
            set(self._test_ids(randomized_suite)))
 
1740
        # Technically, this *can* fail, because random.shuffle(list) can be
 
1741
        # equal to list. Trying multiple times just pushes the frequency back.
 
1742
        # As its len(self.all_names)!:1, the failure frequency should be low
 
1743
        # enough to ignore. RBC 20071021.
 
1744
        # It should change the order.
 
1745
        self.assertNotEqual(self.all_names, self._test_ids(randomized_suite))
 
1746
        # But not the length. (Possibly redundant with the set test, but not
 
1747
        # necessarily.)
 
1748
        self.assertEqual(len(self.all_names),
 
1749
            len(self._test_ids(randomized_suite)))
 
1750
 
1671
1751
    def test_sort_suite_by_re(self):
1672
 
        sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
1673
 
        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
 
1752
        sorted_suite = self.applyDeprecated(one_zero,
 
1753
            sort_suite_by_re, self.suite, 'test_filter_suite_by_r')
 
1754
        sorted_names = self._test_ids(sorted_suite)
1674
1755
        self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1675
1756
            'TestSelftestFiltering.test_filter_suite_by_re')
1676
1757
        self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1677
1758
 
 
1759
    def test_split_suit_by_re(self):
 
1760
        self.all_names = self._test_ids(self.suite)
 
1761
        split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
 
1762
        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
 
1763
            'test_filter_suite_by_re')
 
1764
        self.assertEqual([filtered_name], self._test_ids(split_suite[0]))
 
1765
        self.assertFalse(filtered_name in self._test_ids(split_suite[1]))
 
1766
        remaining_names = list(self.all_names)
 
1767
        remaining_names.remove(filtered_name)
 
1768
        self.assertEqual(remaining_names, self._test_ids(split_suite[1]))
 
1769
 
1678
1770
 
1679
1771
class TestCheckInventoryShape(TestCaseWithTransport):
1680
1772
 
1714
1806
        out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
1715
1807
        self.assertEqual(out, '')
1716
1808
        self.assertEqual(err, 'bzr: ERROR: Not a branch: "/nonexistantpath/".\n')
 
1809
 
 
1810
 
 
1811
class TestTestLoader(TestCase):
 
1812
    """Tests for the test loader."""
 
1813
 
 
1814
    def _get_loader_and_module(self):
 
1815
        """Gets a TestLoader and a module with one test in it."""
 
1816
        loader = TestUtil.TestLoader()
 
1817
        module = {}
 
1818
        class Stub(TestCase):
 
1819
            def test_foo(self):
 
1820
                pass
 
1821
        class MyModule(object):
 
1822
            pass
 
1823
        MyModule.a_class = Stub
 
1824
        module = MyModule()
 
1825
        return loader, module
 
1826
 
 
1827
    def test_module_no_load_tests_attribute_loads_classes(self):
 
1828
        loader, module = self._get_loader_and_module()
 
1829
        self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
 
1830
 
 
1831
    def test_module_load_tests_attribute_gets_called(self):
 
1832
        loader, module = self._get_loader_and_module()
 
1833
        # 'self' is here because we're faking the module with a class. Regular
 
1834
        # load_tests do not need that :)
 
1835
        def load_tests(self, standard_tests, module, loader):
 
1836
            result = loader.suiteClass()
 
1837
            for test in iter_suite_tests(standard_tests):
 
1838
                result.addTests([test, test])
 
1839
            return result
 
1840
        # add a load_tests() method which multiplies the tests from the module.
 
1841
        module.__class__.load_tests = load_tests
 
1842
        self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
 
1843