[Python-checkins] bpo-34945: Buffer output in test suite only when creating junit file (GH-10204)

Miss Islington (bot) webhook-mailer at python.org
Mon Oct 29 17:07:54 EDT 2018

commit: 7b689c68ab3236455f7d58954435d029e2bb2c8a
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-10-29T14:07:50-07:00

bpo-34945: Buffer output in test suite only when creating junit file (GH-10204)

After commit d0f49d2f5085ca68e3dc8725f1fb1c9674bfb5ed, the output of the
test suite is always buffered as the test output needs to be included in
the JUnit file in same cases (as when a test fails). This has the
consequence that printing or using debuggers (like pdb) in the test
suite does not result in a good user experience anymore.

This commit modifies the test suite runner so it only captures the test
output when the JUnit file is requested to fix the regression so prints
and debuggers are usable again.
(cherry picked from commit 02277482ea765335e497ecd9661d4bde9b5ddc67)

Co-authored-by: Pablo Galindo <Pablogsal at gmail.com>

M Lib/test/support/__init__.py
M Lib/test/support/testresult.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 3f1962fe1cd2..7c16c985db28 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1907,10 +1907,9 @@ def _filter_suite(suite, pred):
 def _run_suite(suite):
     """Run tests from a unittest.TestSuite-derived class."""
-    runner = get_test_runner(sys.stdout, verbosity=verbose)
-    # TODO: Remove this before merging (here for easy comparison with old impl)
-    #runner = unittest.TextTestRunner(sys.stdout, verbosity=2, failfast=failfast)
+    runner = get_test_runner(sys.stdout,
+                             verbosity=verbose,
+                             capture_output=(junit_xml_list is not None))
     result = runner.run(suite)
diff --git a/Lib/test/support/testresult.py b/Lib/test/support/testresult.py
index 8988d3d15278..67e126dcf752 100644
--- a/Lib/test/support/testresult.py
+++ b/Lib/test/support/testresult.py
@@ -60,10 +60,12 @@ def _add_result(self, test, capture=False, **args):
             e.set('time', f'{time.perf_counter() - self.__start_time:0.6f}')
         if capture:
-            stdout = self._stdout_buffer.getvalue().rstrip()
-            ET.SubElement(e, 'system-out').text = stdout
-            stderr = self._stderr_buffer.getvalue().rstrip()
-            ET.SubElement(e, 'system-err').text = stderr
+            if self._stdout_buffer is not None:
+                stdout = self._stdout_buffer.getvalue().rstrip()
+                ET.SubElement(e, 'system-out').text = stdout
+            if self._stderr_buffer is not None:
+                stderr = self._stderr_buffer.getvalue().rstrip()
+                ET.SubElement(e, 'system-err').text = stderr
         for k, v in args.items():
             if not k or not v:
@@ -152,23 +154,24 @@ def get_xml_element(self):
         return e
 class QuietRegressionTestRunner:
-    def __init__(self, stream):
+    def __init__(self, stream, buffer=False):
         self.result = RegressionTestResult(stream, None, 0)
+        self.result.buffer = buffer
     def run(self, test):
         return self.result
-def get_test_runner_class(verbosity):
+def get_test_runner_class(verbosity, buffer=False):
     if verbosity:
         return functools.partial(unittest.TextTestRunner,
-                                 buffer=True,
+                                 buffer=buffer,
-    return QuietRegressionTestRunner
+    return functools.partial(QuietRegressionTestRunner, buffer=buffer)
-def get_test_runner(stream, verbosity):
-    return get_test_runner_class(verbosity)(stream)
+def get_test_runner(stream, verbosity, capture_output=False):
+    return get_test_runner_class(verbosity, capture_output)(stream)
 if __name__ == '__main__':
     class TestTests(unittest.TestCase):

More information about the Python-checkins mailing list