summaryrefslogtreecommitdiffstats
path: root/Lib/test/support
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2018-10-29 20:09:41 (GMT)
committerGitHub <noreply@github.com>2018-10-29 20:09:41 (GMT)
commit02277482ea765335e497ecd9661d4bde9b5ddc67 (patch)
treec7e888350dc8bd0232467048b560bc404fe3d880 /Lib/test/support
parent541497e6197268517b0d492856027774c43e0949 (diff)
downloadcpython-02277482ea765335e497ecd9661d4bde9b5ddc67.zip
cpython-02277482ea765335e497ecd9661d4bde9b5ddc67.tar.gz
cpython-02277482ea765335e497ecd9661d4bde9b5ddc67.tar.bz2
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.
Diffstat (limited to 'Lib/test/support')
-rw-r--r--Lib/test/support/__init__.py7
-rw-r--r--Lib/test/support/testresult.py23
2 files changed, 16 insertions, 14 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 01e8935..f7a60d4 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -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)
diff --git a/Lib/test/support/testresult.py b/Lib/test/support/testresult.py
index 8988d3d..67e126d 100644
--- a/Lib/test/support/testresult.py
+++ b/Lib/test/support/testresult.py
@@ -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):