Index: Lib/unittest.py =================================================================== --- Lib/unittest.py (revision 87572) +++ Lib/unittest.py (working copy) @@ -1464,7 +1464,9 @@ It prints out the names of tests as they are run, errors as they occur, and a summary of the results at the end of the test run. """ - def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1): + def __init__(self, stream=None, descriptions=1, verbosity=1): + if stream is None: + stream = sys.stderr self.stream = _WritelnDecorator(stream) self.descriptions = descriptions self.verbosity = verbosity Index: Lib/test/test_unittest.py =================================================================== --- Lib/test/test_unittest.py (revision 87572) +++ Lib/test/test_unittest.py (working copy) @@ -7,6 +7,7 @@ """ import re +import sys from test import support import unittest from unittest import TestCase, TestProgram @@ -3346,7 +3347,21 @@ expected = ['startTestRun', 'stopTestRun'] self.assertEqual(events, expected) + def testStdErrLookedUpAtInstantiationTime(self): + # see issue 10786 + old_stderr = sys.stderr + f = io.StringIO() + sys.stderr = f + runner = unittest.TextTestRunner() + self.assertTrue(runner.stream.stream is f) + sys.stderr = old_stderr + def testSpecifiedStreamUsed(self): + # see issue 10786 + f = io.StringIO() + runner = unittest.TextTestRunner(f) + self.assertTrue(runner.stream.stream is f) + ###################################################################### ## Main ###################################################################### @@ -3355,7 +3370,7 @@ support.run_unittest(Test_TestCase, Test_TestLoader, Test_TestSuite, Test_TestResult, Test_FunctionTestCase, Test_TestSkipping, Test_Assertions, TestLongMessage, - Test_TestProgram, TestCleanUp) + Test_TestProgram, TestCleanUp, Test_TextTestRunner) if __name__ == "__main__": test_main()