TestResult stores original sys.stdout and tests no longer use sys.__stdout__ (etc) in tests for unittest -b command line option

This commit is contained in:
Michael Foord 2010-04-02 22:08:29 +00:00
parent 19479fa437
commit 58c1e78806
2 changed files with 18 additions and 19 deletions

View File

@ -53,6 +53,8 @@ class TestResult(object):
self.buffer = False
self._stdout_buffer = StringIO()
self._stderr_buffer = StringIO()
self._original_stdout = sys.stdout
self._original_stderr = sys.stderr
self._mirrorOutput = False
def printErrors(self):
@ -81,11 +83,11 @@ class TestResult(object):
if output:
if not output.endswith(NEWLINE):
output += NEWLINE
sys.__stdout__.write(STDOUT_LINE % output)
self._original_stdout.write(STDOUT_LINE % output)
if error:
if not error.endswith(NEWLINE):
error += NEWLINE
sys.__stderr__.write(STDERR_LINE % error)
self._original_stderr.write(STDERR_LINE % error)
sys.stdout = _std_out
sys.stderr = _std_err

View File

@ -362,12 +362,12 @@ class Test_OldTestResult(unittest.TestCase):
class TestOutputBuffering(unittest.TestCase):
def setUp(self):
self._real_out = sys.__stdout__
self._real_err = sys.__stderr__
self._real_out = sys.stdout
self._real_err = sys.stderr
def tearDown(self):
sys.stdout = sys.__stdout__ = self._real_out
sys.stderr = sys.__stderr__ = self._real_err
sys.stdout = self._real_out
sys.stderr = self._real_err
def testBufferOutputOff(self):
real_out = self._real_out
@ -407,8 +407,8 @@ class TestOutputBuffering(unittest.TestCase):
out_stream = sys.stdout
err_stream = sys.stderr
sys.__stdout__ = StringIO()
sys.__stderr__ = StringIO()
result._original_stdout = StringIO()
result._original_stderr = StringIO()
print 'foo'
print >> sys.stderr, 'bar'
@ -416,8 +416,8 @@ class TestOutputBuffering(unittest.TestCase):
self.assertEqual(out_stream.getvalue(), 'foo\n')
self.assertEqual(err_stream.getvalue(), 'bar\n')
self.assertEqual(sys.__stdout__.getvalue(), '')
self.assertEqual(sys.__stderr__.getvalue(), '')
self.assertEqual(result._original_stdout.getvalue(), '')
self.assertEqual(result._original_stderr.getvalue(), '')
result.addSuccess(self)
result.stopTest(self)
@ -425,8 +425,8 @@ class TestOutputBuffering(unittest.TestCase):
self.assertIs(real_out, sys.stdout)
self.assertIs(real_err, sys.stderr)
self.assertEqual(sys.__stdout__.getvalue(), '')
self.assertEqual(sys.__stderr__.getvalue(), '')
self.assertEqual(result._original_stdout.getvalue(), '')
self.assertEqual(result._original_stderr.getvalue(), '')
self.assertEqual(out_stream.getvalue(), '')
self.assertEqual(err_stream.getvalue(), '')
@ -439,20 +439,17 @@ class TestOutputBuffering(unittest.TestCase):
return result
def testBufferOutputAddErrorOrFailure(self):
def clear():
sys.__stdout__ = StringIO()
sys.__stderr__ = StringIO()
for message_attr, add_attr, include_error in [
('errors', 'addError', True),
('failures', 'addFailure', False),
('errors', 'addError', True),
('failures', 'addFailure', False)
]:
clear()
result = self.getStartedResult()
buffered_out = sys.stdout
buffered_err = sys.stderr
result._original_stdout = StringIO()
result._original_stderr = StringIO()
print >> sys.stdout, 'foo'
if include_error:
@ -480,8 +477,8 @@ class TestOutputBuffering(unittest.TestCase):
expectedFullMessage = 'None\n%s%s' % (expectedOutMessage, expectedErrMessage)
self.assertIs(test, self)
self.assertEqual(sys.__stdout__.getvalue(), expectedOutMessage)
self.assertEqual(sys.__stderr__.getvalue(), expectedErrMessage)
self.assertEqual(result._original_stdout.getvalue(), expectedOutMessage)
self.assertEqual(result._original_stderr.getvalue(), expectedErrMessage)
self.assertMultiLineEqual(message, expectedFullMessage)
if __name__ == '__main__':