Rev 4581: (robertc) Add support for subunits TestResult.progress() protocol, in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Mon Aug 3 05:47:18 BST 2009


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 4581 [merge]
revision-id: pqm at pqm.ubuntu.com-20090803044716-k5oyle1gq2a7ioxg
parent: pqm at pqm.ubuntu.com-20090731162211-zvddnooijve9nbmu
parent: robertc at robertcollins.net-20090803034432-md3ds838h9rqazqy
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Mon 2009-08-03 05:47:16 +0100
message:
  (robertc) Add support for subunits TestResult.progress() protocol,
  	allowing reuse of bzr's test runner by subunit. (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	2009-07-31 16:22:11 +0000
+++ b/NEWS	2009-08-03 04:47:16 +0000
@@ -160,6 +160,13 @@
 * --subunit support now adds timestamps if the subunit version supports
   it. (Robert Collins)
 
+* The ``bzrlib.tests.TextTestRunner`` will no longer call
+  ``countTestsCases`` on the test being run. Progress information is
+  instead handled by having the test passed in call ``result.progress``
+  before running its contents. This improves the behaviour when using
+  ``TextTestRunner`` with test suites that don't support
+  ``countTestsCases``. (Robert Collins)
+
 
 bzr 1.17 "So late it's brunch" 2009-07-20
 #########################################

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2009-07-23 04:46:26 +0000
+++ b/bzrlib/tests/__init__.py	2009-08-03 03:44:32 +0000
@@ -113,6 +113,10 @@
 
 default_transport = LocalURLServer
 
+# Subunit result codes, defined here to prevent a hard dependency on subunit.
+SUBUNIT_SEEK_SET = 0
+SUBUNIT_SEEK_CUR = 1
+
 
 class ExtendedTestResult(unittest._TextTestResult):
     """Accepts, reports and accumulates the results of running tests.
@@ -134,7 +138,6 @@
 
     def __init__(self, stream, descriptions, verbosity,
                  bench_history=None,
-                 num_tests=None,
                  strict=False,
                  ):
         """Construct new TestResult.
@@ -159,7 +162,7 @@
             bench_history.write("--date %s %s\n" % (time.time(), revision_id))
         self._bench_history = bench_history
         self.ui = ui.ui_factory
-        self.num_tests = num_tests
+        self.num_tests = 0
         self.error_count = 0
         self.failure_count = 0
         self.known_failure_count = 0
@@ -359,6 +362,15 @@
             self.stream.writeln(self.separator2)
             self.stream.writeln("%s" % err)
 
+    def progress(self, offset, whence):
+        """The test is adjusting the count of tests to run."""
+        if whence == SUBUNIT_SEEK_SET:
+            self.num_tests = offset
+        elif whence == SUBUNIT_SEEK_CUR:
+            self.num_tests += offset
+        else:
+            raise errors.BzrError("Unknown whence %r" % whence)
+
     def finished(self):
         pass
 
@@ -379,12 +391,11 @@
 
     def __init__(self, stream, descriptions, verbosity,
                  bench_history=None,
-                 num_tests=None,
                  pb=None,
                  strict=None,
                  ):
         ExtendedTestResult.__init__(self, stream, descriptions, verbosity,
-            bench_history, num_tests, strict)
+            bench_history, strict)
         if pb is None:
             self.pb = self.ui.nested_progress_bar()
             self._supplied_pb = False
@@ -410,7 +421,7 @@
         ##     a += ', %d skip' % self.skip_count
         ## if self.known_failure_count:
         ##     a += '+%dX' % self.known_failure_count
-        if self.num_tests is not None:
+        if self.num_tests:
             a +='/%d' % self.num_tests
         a += ' in '
         runtime = time.time() - self._overall_start_time
@@ -566,7 +577,6 @@
                               self.descriptions,
                               self.verbosity,
                               bench_history=self._bench_history,
-                              num_tests=test.countTestCases(),
                               strict=self._strict,
                               )
         result.stop_early = self.stop_on_failure
@@ -2752,6 +2762,8 @@
         decorators.append(filter_tests(pattern))
     if suite_decorators:
         decorators.extend(suite_decorators)
+    # tell the result object how many tests will be running:
+    decorators.append(CountingDecorator)
     for decorator in decorators:
         suite = decorator(suite)
     result = runner.run(suite)
@@ -2861,6 +2873,16 @@
         return result
 
 
+class CountingDecorator(TestDecorator):
+    """A decorator which calls result.progress(self.countTestCases)."""
+
+    def run(self, result):
+        progress_method = getattr(result, 'progress', None)
+        if callable(progress_method):
+            progress_method(self.countTestCases(), SUBUNIT_SEEK_SET)
+        return super(CountingDecorator, self).run(result)
+
+
 class ExcludeDecorator(TestDecorator):
     """A decorator which excludes test matching an exclude pattern."""
 

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2009-07-22 06:00:45 +0000
+++ b/bzrlib/tests/test_selftest.py	2009-08-03 01:57:07 +0000
@@ -1486,8 +1486,7 @@
         result = bzrlib.tests.VerboseTestResult(
             unittest._WritelnDecorator(output_stream),
             descriptions=0,
-            verbosity=2,
-            num_tests=sample_test.countTestCases())
+            verbosity=2)
         sample_test.run(result)
         self.assertContainsRe(
             output_stream.getvalue(),
@@ -2368,7 +2367,7 @@
                 return tests.ExtendedTestResult(self.stream, self.descriptions,
                                                 self.verbosity)
         tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
-        self.assertEqual(calls, [suite])
+        self.assertLength(1, calls)
 
     def test_done(self):
         """run_suite should call result.done()"""




More information about the bazaar-commits mailing list