Skip to content

Commit 3f0b91f

Browse files
author
Michael Foord
committed
Issue 10786: unittest.TextTestRunner default stream no longer bound at import time
1 parent 311dabf commit 3f0b91f

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/unittest/runner.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ class TextTestRunner(object):
125125
"""
126126
resultclass = TextTestResult
127127

128-
def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1,
128+
def __init__(self, stream=None, descriptions=True, verbosity=1,
129129
failfast=False, buffer=False, resultclass=None, warnings=None):
130+
if stream is None:
131+
stream = sys.stderr
130132
self.stream = _WritelnDecorator(stream)
131133
self.descriptions = descriptions
132134
self.verbosity = verbosity

Lib/unittest/test/test_runner.py

+17
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,20 @@ def get_parse_out_err(p):
299299
self.assertEqual(out.count(msg), 3)
300300
for msg in [ae_msg, at_msg]:
301301
self.assertEqual(out.count(msg), 1)
302+
303+
def testStdErrLookedUpAtInstantiationTime(self):
304+
# see issue 10786
305+
old_stderr = sys.stderr
306+
f = io.StringIO()
307+
sys.stderr = f
308+
try:
309+
runner = unittest.TextTestRunner()
310+
self.assertTrue(runner.stream.stream is f)
311+
finally:
312+
sys.stderr = old_stderr
313+
314+
def testSpecifiedStreamUsed(self):
315+
# see issue 10786
316+
f = io.StringIO()
317+
runner = unittest.TextTestRunner(f)
318+
self.assertTrue(runner.stream.stream is f)

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Core and Builtins
2020
Library
2121
-------
2222

23+
- Issue 10786: unittest.TextTestRunner default stream no longer bound at
24+
import time. `sys.stderr` now looked up at instantiation time. Fix contributed
25+
by Mark Roddy.
26+
2327
- Issue 10753 - Characters ';','=' and ',' in the PATH_INFO environment
2428
variable won't be quoted when the URI is constructed by the wsgiref.util 's
2529
request_uri method. According to RFC 3986, these characters can be a part of

0 commit comments

Comments
 (0)