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

After commit d0f49d2f50, 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.
This commit is contained in:
Pablo Galindo 2018-10-29 16:09:41 -04:00 committed by GitHub
parent 541497e619
commit 02277482ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 14 deletions

View 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)

View 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):