mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
[3.10] bpo-13236: Flush the output stream more often in unittest (GH-29929) (GH-30039)
It can prevent some losses when output to buffered stream..
(cherry picked from commit 83fa1291fd
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
9299e3a39c
commit
d55a03e02e
4 changed files with 44 additions and 4 deletions
|
@ -59,6 +59,7 @@ class TextTestResult(result.TestResult):
|
|||
super(TextTestResult, self).addSuccess(test)
|
||||
if self.showAll:
|
||||
self.stream.writeln("ok")
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write('.')
|
||||
self.stream.flush()
|
||||
|
@ -67,6 +68,7 @@ class TextTestResult(result.TestResult):
|
|||
super(TextTestResult, self).addError(test, err)
|
||||
if self.showAll:
|
||||
self.stream.writeln("ERROR")
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write('E')
|
||||
self.stream.flush()
|
||||
|
@ -75,6 +77,7 @@ class TextTestResult(result.TestResult):
|
|||
super(TextTestResult, self).addFailure(test, err)
|
||||
if self.showAll:
|
||||
self.stream.writeln("FAIL")
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write('F')
|
||||
self.stream.flush()
|
||||
|
@ -83,6 +86,7 @@ class TextTestResult(result.TestResult):
|
|||
super(TextTestResult, self).addSkip(test, reason)
|
||||
if self.showAll:
|
||||
self.stream.writeln("skipped {0!r}".format(reason))
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write("s")
|
||||
self.stream.flush()
|
||||
|
@ -91,6 +95,7 @@ class TextTestResult(result.TestResult):
|
|||
super(TextTestResult, self).addExpectedFailure(test, err)
|
||||
if self.showAll:
|
||||
self.stream.writeln("expected failure")
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write("x")
|
||||
self.stream.flush()
|
||||
|
@ -99,6 +104,7 @@ class TextTestResult(result.TestResult):
|
|||
super(TextTestResult, self).addUnexpectedSuccess(test)
|
||||
if self.showAll:
|
||||
self.stream.writeln("unexpected success")
|
||||
self.stream.flush()
|
||||
elif self.dots:
|
||||
self.stream.write("u")
|
||||
self.stream.flush()
|
||||
|
@ -106,6 +112,7 @@ class TextTestResult(result.TestResult):
|
|||
def printErrors(self):
|
||||
if self.dots or self.showAll:
|
||||
self.stream.writeln()
|
||||
self.stream.flush()
|
||||
self.printErrorList('ERROR', self.errors)
|
||||
self.printErrorList('FAIL', self.failures)
|
||||
|
||||
|
@ -115,6 +122,7 @@ class TextTestResult(result.TestResult):
|
|||
self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
|
||||
self.stream.writeln(self.separator2)
|
||||
self.stream.writeln("%s" % err)
|
||||
self.stream.flush()
|
||||
|
||||
|
||||
class TextTestRunner(object):
|
||||
|
@ -218,4 +226,5 @@ class TextTestRunner(object):
|
|||
self.stream.writeln(" (%s)" % (", ".join(infos),))
|
||||
else:
|
||||
self.stream.write("\n")
|
||||
self.stream.flush()
|
||||
return result
|
||||
|
|
|
@ -6,6 +6,7 @@ import subprocess
|
|||
from test import support
|
||||
import unittest
|
||||
import unittest.test
|
||||
from .test_result import BufferedWriter
|
||||
|
||||
|
||||
class Test_TestProgram(unittest.TestCase):
|
||||
|
@ -104,30 +105,39 @@ class Test_TestProgram(unittest.TestCase):
|
|||
program.testNames)
|
||||
|
||||
def test_NonExit(self):
|
||||
stream = BufferedWriter()
|
||||
program = unittest.main(exit=False,
|
||||
argv=["foobar"],
|
||||
testRunner=unittest.TextTestRunner(stream=io.StringIO()),
|
||||
testRunner=unittest.TextTestRunner(stream=stream),
|
||||
testLoader=self.FooBarLoader())
|
||||
self.assertTrue(hasattr(program, 'result'))
|
||||
self.assertIn('\nFAIL: testFail ', stream.getvalue())
|
||||
self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
|
||||
|
||||
|
||||
def test_Exit(self):
|
||||
stream = BufferedWriter()
|
||||
self.assertRaises(
|
||||
SystemExit,
|
||||
unittest.main,
|
||||
argv=["foobar"],
|
||||
testRunner=unittest.TextTestRunner(stream=io.StringIO()),
|
||||
testRunner=unittest.TextTestRunner(stream=stream),
|
||||
exit=True,
|
||||
testLoader=self.FooBarLoader())
|
||||
self.assertIn('\nFAIL: testFail ', stream.getvalue())
|
||||
self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
|
||||
|
||||
|
||||
def test_ExitAsDefault(self):
|
||||
stream = BufferedWriter()
|
||||
self.assertRaises(
|
||||
SystemExit,
|
||||
unittest.main,
|
||||
argv=["foobar"],
|
||||
testRunner=unittest.TextTestRunner(stream=io.StringIO()),
|
||||
testRunner=unittest.TextTestRunner(stream=stream),
|
||||
testLoader=self.FooBarLoader())
|
||||
self.assertIn('\nFAIL: testFail ', stream.getvalue())
|
||||
self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
|
||||
|
||||
|
||||
class InitialisableProgram(unittest.TestProgram):
|
||||
|
|
|
@ -34,6 +34,22 @@ def bad_cleanup2():
|
|||
raise ValueError('bad cleanup2')
|
||||
|
||||
|
||||
class BufferedWriter:
|
||||
def __init__(self):
|
||||
self.result = ''
|
||||
self.buffer = ''
|
||||
|
||||
def write(self, arg):
|
||||
self.buffer += arg
|
||||
|
||||
def flush(self):
|
||||
self.result += self.buffer
|
||||
self.buffer = ''
|
||||
|
||||
def getvalue(self):
|
||||
return self.result
|
||||
|
||||
|
||||
class Test_TestResult(unittest.TestCase):
|
||||
# Note: there are not separate tests for TestResult.wasSuccessful(),
|
||||
# TestResult.errors, TestResult.failures, TestResult.testsRun or
|
||||
|
@ -445,10 +461,13 @@ class Test_TestResult(unittest.TestCase):
|
|||
self.assertTrue(result.shouldStop)
|
||||
|
||||
def testFailFastSetByRunner(self):
|
||||
runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True)
|
||||
stream = BufferedWriter()
|
||||
runner = unittest.TextTestRunner(stream=stream, failfast=True)
|
||||
def test(result):
|
||||
self.assertTrue(result.failfast)
|
||||
result = runner.run(test)
|
||||
stream.flush()
|
||||
self.assertTrue(stream.getvalue().endswith('\n\nOK\n'))
|
||||
|
||||
|
||||
classDict = dict(unittest.TestResult.__dict__)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
:class:`unittest.TextTestResult` and :class:`unittest.TextTestRunner` flush
|
||||
now the output stream more often.
|
Loading…
Add table
Add a link
Reference in a new issue