Rev 2922: * New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test in http://people.ubuntu.com/~robertc/baz2.0/in-module-parameterisation

Robert Collins robertc at robertcollins.net
Sat Oct 20 23:33:27 BST 2007


At http://people.ubuntu.com/~robertc/baz2.0/in-module-parameterisation

------------------------------------------------------------
revno: 2922
revision-id: robertc at robertcollins.net-20071020223257-bu5r35znenkw9ups
parent: pqm at pqm.ubuntu.com-20071019201226-6z006xotgfe7zmu8
committer: Robert Collins <robertc at robertcollins.net>
branch nick: in-module-parameterisation
timestamp: Sun 2007-10-21 08:32:57 +1000
message:
  * New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
    suite into two according to a regular expression. (Robert Collins)
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
=== modified file 'NEWS'
--- a/NEWS	2007-10-19 18:43:47 +0000
+++ b/NEWS	2007-10-20 22:32:57 +0000
@@ -78,7 +78,6 @@
    * Show encodings, locale and list of plugins in the traceback message.
      (Martin Pool, #63894)
 
-
   DOCUMENTATION:
 
    * New *Bazaar in Five Minutes* guide.  (Matthew Revell)
@@ -264,6 +263,9 @@
 
   TESTING:
 
+   * New helper method ``bzrlib.tests.split_suite_by_re`` which splits a test
+     suite into two according to a regular expression. (Robert Collins)
+
    * New transport implementation ``trace+`` which is useful for testing,
      logging activity taken to its _activity attribute. (Robert Collins)
 

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-10-16 17:37:43 +0000
+++ b/bzrlib/tests/__init__.py	2007-10-20 22:32:57 +0000
@@ -2223,22 +2223,51 @@
                             just an ordering directive
     :returns: the newly created suite
     """ 
-    first = []
-    second = []
+    if append_rest:
+        suites = split_suite_by_re(suite, pattern)
+    else:
+        suites = [suite]
+    out_tests = []
+    for suite in suites:
+        result = []
+        filter_re = re.compile(pattern)
+        if exclude_pattern is not None:
+            exclude_re = re.compile(exclude_pattern)
+        for test in iter_suite_tests(suite):
+            test_id = test.id()
+            if exclude_pattern is None or not exclude_re.search(test_id):
+                if append_rest:
+                    result.append(test)
+                elif filter_re.search(test_id):
+                    result.append(test)
+        if random_order:
+            random.shuffle(result)
+        out_tests.extend(result)
+    return TestUtil.TestSuite(out_tests)
+
+
+def split_suite_by_re(suite, pattern):
+    """Split a test suite into two by a regular expression.
+    
+    :param suite: The suite to split.
+    :param pattern: A regular expression string. Test ids that match this
+        pattern will be in the first test suite returned, and the others in the
+        second test suite returned.
+    :return: A tuple of two test suites, where the first contains tests from
+        suite matching pattern, and the second contains the remainder from
+        suite. The order within each output suite is the same as it was in
+        suite.
+    """ 
+    matched = []
+    did_not_match = []
     filter_re = re.compile(pattern)
-    if exclude_pattern is not None:
-        exclude_re = re.compile(exclude_pattern)
     for test in iter_suite_tests(suite):
         test_id = test.id()
-        if exclude_pattern is None or not exclude_re.search(test_id):
-            if filter_re.search(test_id):
-                first.append(test)
-            elif append_rest:
-                second.append(test)
-    if random_order:
-        random.shuffle(first)
-        random.shuffle(second)
-    return TestUtil.TestSuite(first + second)
+        if filter_re.search(test_id):
+            matched.append(test)
+        else:
+            did_not_match.append(test)
+    return TestUtil.TestSuite(matched), TestUtil.TestSuite(did_not_match)
 
 
 def run_suite(suite, name='test', verbose=False, pattern=".*",

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-10-02 11:24:53 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-10-20 22:32:57 +0000
@@ -57,6 +57,7 @@
                           iter_suite_tests,
                           filter_suite_by_re,
                           sort_suite_by_re,
+                          split_suite_by_re,
                           test_lsprof,
                           test_suite,
                           )
@@ -1644,21 +1645,36 @@
         self.loader = TestUtil.TestLoader()
         self.suite.addTest(self.loader.loadTestsFromModuleNames([
             'bzrlib.tests.test_selftest']))
-        self.all_names = [t.id() for t in iter_suite_tests(self.suite)]
+        self.all_names = self._test_ids(self.suite)
+
+    def _test_ids(self, test_suite):
+        """Get the ids for the tests in a test suite."""
+        return [t.id() for t in iter_suite_tests(test_suite)]
 
     def test_filter_suite_by_re(self):
         filtered_suite = filter_suite_by_re(self.suite, 'test_filter')
-        filtered_names = [t.id() for t in iter_suite_tests(filtered_suite)]
+        filtered_names = self._test_ids(filtered_suite)
         self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
             'TestSelftestFiltering.test_filter_suite_by_re'])
-            
+
     def test_sort_suite_by_re(self):
         sorted_suite = sort_suite_by_re(self.suite, 'test_filter')
-        sorted_names = [t.id() for t in iter_suite_tests(sorted_suite)]
+        sorted_names = self._test_ids(sorted_suite)
         self.assertEqual(sorted_names[0], 'bzrlib.tests.test_selftest.'
             'TestSelftestFiltering.test_filter_suite_by_re')
         self.assertEquals(sorted(self.all_names), sorted(sorted_names))
 
+    def test_split_suit_by_re(self):
+        self.all_names = self._test_ids(self.suite)
+        split_suite = split_suite_by_re(self.suite, 'test_filter')
+        filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
+            'test_filter_suite_by_re')
+        self.assertEqual([filtered_name], self._test_ids(split_suite[0]))
+        self.assertFalse(filtered_name in self._test_ids(split_suite[1]))
+        remaining_names = list(self.all_names)
+        remaining_names.remove(filtered_name)
+        self.assertEqual(remaining_names, self._test_ids(split_suite[1]))
+
 
 class TestCheckInventoryShape(TestCaseWithTransport):
 



More information about the bazaar-commits mailing list