1723
1728
self.loader = TestUtil.TestLoader()
1724
1729
self.suite.addTest(self.loader.loadTestsFromModuleNames([
1725
1730
'bzrlib.tests.test_selftest']))
1726
self.all_names = self._test_ids(self.suite)
1728
def _test_ids(self, test_suite):
1729
"""Get the ids for the tests in a test suite."""
1730
return [t.id() for t in iter_suite_tests(test_suite)]
1731
self.all_names = _test_ids(self.suite)
1732
1733
def test_condition_id_re(self):
1733
1734
test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1734
1735
'test_condition_id_re')
1735
1736
filtered_suite = filter_suite_by_condition(self.suite,
1736
1737
condition_id_re('test_condition_id_re'))
1737
self.assertEqual([test_name], self._test_ids(filtered_suite))
1738
self.assertEqual([test_name], _test_ids(filtered_suite))
1739
1740
def test_condition_id_in_list(self):
1740
1741
test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
1744
1745
self.suite, tests.condition_id_in_list(id_list))
1745
1746
my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
1746
1747
re_filtered = filter_suite_by_re(self.suite, my_pattern)
1747
self.assertEqual(self._test_ids(re_filtered),
1748
self._test_ids(filtered_suite))
1748
self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1750
1750
def test_condition_isinstance(self):
1751
1751
filtered_suite = filter_suite_by_condition(self.suite,
1752
1752
condition_isinstance(self.__class__))
1753
1753
class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1754
1754
re_filtered = filter_suite_by_re(self.suite, class_pattern)
1755
self.assertEqual(self._test_ids(re_filtered),
1756
self._test_ids(filtered_suite))
1755
self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1758
1757
def test_exclude_tests_by_condition(self):
1759
1758
excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1762
1761
lambda x:x.id() == excluded_name)
1763
1762
self.assertEqual(len(self.all_names) - 1,
1764
1763
filtered_suite.countTestCases())
1765
self.assertFalse(excluded_name in self._test_ids(filtered_suite))
1764
self.assertFalse(excluded_name in _test_ids(filtered_suite))
1766
1765
remaining_names = list(self.all_names)
1767
1766
remaining_names.remove(excluded_name)
1768
self.assertEqual(remaining_names, self._test_ids(filtered_suite))
1767
self.assertEqual(remaining_names, _test_ids(filtered_suite))
1770
1769
def test_exclude_tests_by_re(self):
1771
self.all_names = self._test_ids(self.suite)
1770
self.all_names = _test_ids(self.suite)
1772
1771
filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
1773
1772
excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1774
1773
'test_exclude_tests_by_re')
1775
1774
self.assertEqual(len(self.all_names) - 1,
1776
1775
filtered_suite.countTestCases())
1777
self.assertFalse(excluded_name in self._test_ids(filtered_suite))
1776
self.assertFalse(excluded_name in _test_ids(filtered_suite))
1778
1777
remaining_names = list(self.all_names)
1779
1778
remaining_names.remove(excluded_name)
1780
self.assertEqual(remaining_names, self._test_ids(filtered_suite))
1779
self.assertEqual(remaining_names, _test_ids(filtered_suite))
1782
1781
def test_filter_suite_by_condition(self):
1783
1782
test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1784
1783
'test_filter_suite_by_condition')
1785
1784
filtered_suite = filter_suite_by_condition(self.suite,
1786
1785
lambda x:x.id() == test_name)
1787
self.assertEqual([test_name], self._test_ids(filtered_suite))
1786
self.assertEqual([test_name], _test_ids(filtered_suite))
1789
1788
def test_filter_suite_by_re(self):
1790
1789
filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
1791
filtered_names = self._test_ids(filtered_suite)
1790
filtered_names = _test_ids(filtered_suite)
1792
1791
self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1793
1792
'TestSelftestFiltering.test_filter_suite_by_re'])
1811
1810
def test_randomize_suite(self):
1812
1811
randomized_suite = randomize_suite(self.suite)
1813
1812
# randomizing should not add or remove test names.
1814
self.assertEqual(set(self._test_ids(self.suite)),
1815
set(self._test_ids(randomized_suite)))
1813
self.assertEqual(set(_test_ids(self.suite)),
1814
set(_test_ids(randomized_suite)))
1816
1815
# Technically, this *can* fail, because random.shuffle(list) can be
1817
1816
# equal to list. Trying multiple times just pushes the frequency back.
1818
1817
# As its len(self.all_names)!:1, the failure frequency should be low
1819
1818
# enough to ignore. RBC 20071021.
1820
1819
# It should change the order.
1821
self.assertNotEqual(self.all_names, self._test_ids(randomized_suite))
1820
self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
1822
1821
# But not the length. (Possibly redundant with the set test, but not
1823
1822
# necessarily.)
1824
self.assertEqual(len(self.all_names),
1825
len(self._test_ids(randomized_suite)))
1823
self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
1827
1825
def test_sort_suite_by_re(self):
1828
1826
sorted_suite = self.applyDeprecated(one_zero,
1829
1827
sort_suite_by_re, self.suite, 'test_filter_suite_by_r')
1830
sorted_names = self._test_ids(sorted_suite)
1828
sorted_names = _test_ids(sorted_suite)
1831
1829
self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
1832
1830
'TestSelftestFiltering.test_filter_suite_by_re')
1833
1831
self.assertEquals(sorted(self.all_names), sorted(sorted_names))
1835
1833
def test_split_suit_by_re(self):
1836
self.all_names = self._test_ids(self.suite)
1834
self.all_names = _test_ids(self.suite)
1837
1835
split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
1838
1836
filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1839
1837
'test_filter_suite_by_re')
1840
self.assertEqual([filtered_name], self._test_ids(split_suite[0]))
1841
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]))
1842
1840
remaining_names = list(self.all_names)
1843
1841
remaining_names.remove(filtered_name)
1844
self.assertEqual(remaining_names, self._test_ids(split_suite[1]))
1842
self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1847
1845
class TestCheckInventoryShape(TestCaseWithTransport):
1918
1916
module.__class__.load_tests = load_tests
1919
1917
self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
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'],
1925
def test_load_tests_from_module_name_with_bogus_module_name(self):
1926
loader = TestUtil.TestLoader()
1927
self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
1922
1930
class TestTestIdList(tests.TestCase):
1993
2001
suite = tests.test_suite(test_list)
1994
self.assertEquals(test_list, self._test_ids(suite))
2002
self.assertEquals(test_list, _test_ids(suite))
1996
2004
def test_test_suite_matches_id_list_with_unknown(self):
1997
2005
loader = TestUtil.TestLoader()
1998
import bzrlib.tests.test_sampler
1999
suite = loader.loadTestsFromModule(bzrlib.tests.test_sampler)
2006
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2000
2007
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
2002
2009
not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
2006
2013
def test_suite_matches_id_list_with_duplicates(self):
2007
2014
loader = TestUtil.TestLoader()
2008
import bzrlib.tests.test_sampler
2009
suite = loader.loadTestsFromModule(bzrlib.tests.test_sampler)
2015
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2010
2016
dupes = loader.suiteClass()
2011
2017
for test in iter_suite_tests(suite):
2012
2018
dupes.addTest(test)