summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/test
diff options
context:
space:
mode:
authorMichael Foord <fuzzyman@voidspace.org.uk>2010-04-02 21:42:47 (GMT)
committerMichael Foord <fuzzyman@voidspace.org.uk>2010-04-02 21:42:47 (GMT)
commit5637f04a94242b6b696cf699625f708ce4ecb186 (patch)
tree0a0b0fe31aca0b938febc93c0ebba9c1c7165a8f /Lib/unittest/test
parent51f2f16ee395449cd669d3378656d338df4ff5f1 (diff)
downloadcpython-5637f04a94242b6b696cf699625f708ce4ecb186.zip
cpython-5637f04a94242b6b696cf699625f708ce4ecb186.tar.gz
cpython-5637f04a94242b6b696cf699625f708ce4ecb186.tar.bz2
Addition of -b command line option to unittest for buffering stdout and stderr during test runs.
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r--Lib/unittest/test/test_break.py10
-rw-r--r--Lib/unittest/test/test_result.py130
2 files changed, 135 insertions, 5 deletions
diff --git a/Lib/unittest/test/test_break.py b/Lib/unittest/test/test_break.py
index 0de31cd..1cd75b8 100644
--- a/Lib/unittest/test/test_break.py
+++ b/Lib/unittest/test/test_break.py
@@ -205,8 +205,9 @@ class TestBreak(unittest.TestCase):
p = Program(False)
p.runTests()
- self.assertEqual(FakeRunner.initArgs, [((), {'verbosity': verbosity,
- 'failfast': failfast})])
+ self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
+ 'verbosity': verbosity,
+ 'failfast': failfast})])
self.assertEqual(FakeRunner.runArgs, [test])
self.assertEqual(p.result, result)
@@ -217,8 +218,9 @@ class TestBreak(unittest.TestCase):
p = Program(True)
p.runTests()
- self.assertEqual(FakeRunner.initArgs, [((), {'verbosity': verbosity,
- 'failfast': failfast})])
+ self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None,
+ 'verbosity': verbosity,
+ 'failfast': failfast})])
self.assertEqual(FakeRunner.runArgs, [test])
self.assertEqual(p.result, result)
diff --git a/Lib/unittest/test/test_result.py b/Lib/unittest/test/test_result.py
index c29fd9c..8637679 100644
--- a/Lib/unittest/test/test_result.py
+++ b/Lib/unittest/test/test_result.py
@@ -1,5 +1,6 @@
import sys
-from cStringIO import StringIO
+import textwrap
+from cStringIO import StringIO, OutputType
from test import test_support
import unittest
@@ -301,6 +302,8 @@ def __init__(self, stream=None, descriptions=None, verbosity=None):
self.errors = []
self.testsRun = 0
self.shouldStop = False
+ self.buffer = False
+
classDict['__init__'] = __init__
OldResult = type('OldResult', (object,), classDict)
@@ -356,5 +359,130 @@ class Test_OldTestResult(unittest.TestCase):
runner.run(Test('testFoo'))
+class TestOutputBuffering(unittest.TestCase):
+
+ def setUp(self):
+ 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
+
+ def testBufferOutputOff(self):
+ real_out = self._real_out
+ real_err = self._real_err
+
+ result = unittest.TestResult()
+ self.assertFalse(result.buffer)
+
+ self.assertIs(real_out, sys.stdout)
+ self.assertIs(real_err, sys.stderr)
+
+ result.startTest(self)
+
+ self.assertIs(real_out, sys.stdout)
+ self.assertIs(real_err, sys.stderr)
+
+ def testBufferOutputStartTestAddSuccess(self):
+ real_out = self._real_out
+ real_err = self._real_err
+
+ result = unittest.TestResult()
+ self.assertFalse(result.buffer)
+
+ result.buffer = True
+
+ self.assertIs(real_out, sys.stdout)
+ self.assertIs(real_err, sys.stderr)
+
+ result.startTest(self)
+
+ self.assertIsNot(real_out, sys.stdout)
+ self.assertIsNot(real_err, sys.stderr)
+ self.assertIsInstance(sys.stdout, OutputType)
+ self.assertIsInstance(sys.stderr, OutputType)
+ self.assertIsNot(sys.stdout, sys.stderr)
+
+ out_stream = sys.stdout
+ err_stream = sys.stderr
+
+ sys.__stdout__ = StringIO()
+ sys.__stderr__ = StringIO()
+
+ print 'foo'
+ print >> sys.stderr, 'bar'
+
+ self.assertEqual(out_stream.getvalue(), 'foo\n')
+ self.assertEqual(err_stream.getvalue(), 'bar\n')
+
+ self.assertEqual(sys.__stdout__.getvalue(), '')
+ self.assertEqual(sys.__stderr__.getvalue(), '')
+
+ result.addSuccess(self)
+ result.stopTest(self)
+
+ self.assertIs(real_out, sys.stdout)
+ self.assertIs(real_err, sys.stderr)
+
+ self.assertEqual(sys.__stdout__.getvalue(), '')
+ self.assertEqual(sys.__stderr__.getvalue(), '')
+
+ self.assertEqual(out_stream.getvalue(), '')
+ self.assertEqual(err_stream.getvalue(), '')
+
+
+ def getStartedResult(self):
+ result = unittest.TestResult()
+ result.buffer = True
+ result.startTest(self)
+ 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
+
+ print >> sys.stdout, 'foo'
+ if include_error:
+ print >> sys.stderr, 'bar'
+
+
+ addFunction = getattr(result, add_attr)
+ addFunction(self, (None, None, None))
+ result.stopTest(self)
+
+ result_list = getattr(result, message_attr)
+ self.assertEqual(len(result_list), 1)
+
+ test, message = result_list[0]
+ expectedOutMessage = textwrap.dedent("""
+ Stdout:
+ foo
+ """)
+ expectedErrMessage = ''
+ if include_error:
+ expectedErrMessage = textwrap.dedent("""
+ Stderr:
+ bar
+ """)
+ expectedFullMessage = 'None\n%s%s' % (expectedOutMessage, expectedErrMessage)
+
+ self.assertIs(test, self)
+ self.assertEqual(sys.__stdout__.getvalue(), expectedOutMessage)
+ self.assertEqual(sys.__stderr__.getvalue(), expectedErrMessage)
+ self.assertMultiLineEqual(message, expectedFullMessage)
+
if __name__ == '__main__':
unittest.main()