]> granicus.if.org Git - python/commitdiff
bpo-34945: Buffer output in test suite only when creating junit file (GH-10204)
authorPablo Galindo <Pablogsal@gmail.com>
Mon, 29 Oct 2018 20:09:41 +0000 (16:09 -0400)
committerGitHub <noreply@github.com>
Mon, 29 Oct 2018 20:09:41 +0000 (16:09 -0400)
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.

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

index 01e8935cc8d7614faf788ac8f265a4e03ed2977c..f7a60d4bf2a3cd5b64abd88b238e1477bc7f4bb0 100644 (file)
@@ -1921,10 +1921,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)
 
index 8988d3d152787136b7c77ffb027906b15d22ff81..67e126dcf7527ab0703370b989016adb44d36151 100644 (file)
@@ -60,10 +60,12 @@ class RegressionTestResult(unittest.TextTestResult):
             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 @@ class RegressionTestResult(unittest.TextTestResult):
         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):
         test(self.result)
         return self.result
 
-def get_test_runner_class(verbosity):
+def get_test_runner_class(verbosity, buffer=False):
     if verbosity:
         return functools.partial(unittest.TextTestRunner,
                                  resultclass=RegressionTestResult,
-                                 buffer=True,
+                                 buffer=buffer,
                                  verbosity=verbosity)
-    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):