This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: unittest.TextTextRunner does not respect redirected stderr
Type: behavior Stage: resolved
Components: Tests Versions: Python 2.7
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: cooyeah, michael.foord, sylvain.corlay
Priority: normal Keywords: easy

Created on 2010-12-28 06:57 by cooyeah, last changed 2022-04-11 14:57 by admin. This issue is now closed.

File name Uploaded Description Edit
py27.patch MarkRoddy, 2010-12-30 17:15 Patch against python 2.7
py32.patch MarkRoddy, 2010-12-30 17:16 Patch agains python 3.2
py31.patch MarkRoddy, 2010-12-30 17:18 Patch against python 3.1
Messages (8)
msg124764 - (view) Author: (cooyeah) Date: 2010-12-28 06:57
The constructor of TextTestRunner class looks like:
def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1)

Since the default parameter is evaluated only once, if sys.stderr is redirected later, the test would still use the old stderr when it was first initialized.
msg124801 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-12-28 16:27
TextTestRunner is initialised with a stream to output messages on that defaults to sys.stderr. The correct way to redirect messages is to construct it with a different stream. 

If you want a redirectable stream then construct the runner with a stream that delegates operations to a 'backend stream' but allows you to redirect it.

Fixing TextTestRunner to dynamically lookup up sys.stderr would not be compatible with systems that redirect sys.stderr but *don't* expect this to prevent test run information from being output.

I suggest closing as wontfix.
msg124802 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-12-28 16:34
Actually I can't see a good reason why not to just lookup the *current* sys.stderr at instantiation time instead of binding at import time as is the current behaviour.

Patch with tests will make it more likely that this change goes in sooner rather than later.
msg124920 - (view) Author: Mark Roddy (MarkRoddy) * Date: 2010-12-30 17:18
All patches change the default value of stream to None in the constructor, and set it to the current to sys.stderr if the argument is None.  Unit tests included to check this behavior.  

Also, the patch against Python 3.1 adds the Test_TextTestRunner test case to the list of tests to be run.  Apparently this test case was not being run.
msg124930 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-12-30 19:37
Committed to py3k in revision 87582.
msg124981 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-12-31 23:15
Since the current behavior matches the current doc,

"class unittest.TextTestRunner(stream=sys.stderr, descriptions=True, verbosity=1, runnerclass=None, warnings=None) 
A basic test runner implementation which prints results on standard error. ..."

this is a feature change request, not a bug report. Hence, the change should not be backported, lest it surprise someone. One could even question whether the change should be introduced now, but is does seem rather minor.

That aside, the doc needs to be changed and a version-changed note added. Something like

"class unittest.TextTestRunner(stream=None, descriptions=True, verbosity=1, runnerclass=None, warnings=None) 
A basic test runner implementation. If *stream* is the default None, results go to standard error. ...
Version changed 3.2: default stream determined when class is instantiated rather than when imported."
msg125163 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-01-03 12:56
Thanks Terry. Done. Doc changes committed revision 87679.
msg192002 - (view) Author: Sylvain Corlay (sylvain.corlay) * Date: 2013-06-28 15:46
It would be great if this modification was also done for Python 2.7. 

A reason is that IPython redirects stderr. When running unit tests in the IPython console without specifying the stream argument, the errors are printed in the shell. 

Date User Action Args
2022-04-11 14:57:10adminsetgithub: 54995
2013-06-28 15:46:10sylvain.corlaysetnosy: + sylvain.corlay

messages: + msg192002
versions: + Python 2.7, - Python 3.2
2011-01-03 12:56:09michael.foordsetstatus: open -> closed

type: enhancement -> behavior

keywords: - patch
nosy: - terry.reedy, MarkRoddy
messages: + msg125163
resolution: accepted
stage: needs patch -> resolved
2010-12-31 23:15:25terry.reedysetversions: - Python 3.1, Python 2.7
nosy: + terry.reedy

messages: + msg124981

type: behavior -> enhancement
2010-12-30 19:37:48michael.foordsetnosy: michael.foord, MarkRoddy, cooyeah
messages: + msg124930
2010-12-30 17:18:28MarkRoddysetfiles: + py31.patch
nosy: + MarkRoddy
messages: + msg124920

2010-12-30 17:16:10MarkRoddysetfiles: + py32.patch
nosy: michael.foord, cooyeah
2010-12-30 17:15:44MarkRoddysetfiles: + py27.patch
nosy: michael.foord, cooyeah
keywords: + patch
2010-12-28 16:34:31michael.foordsetnosy: michael.foord, cooyeah
messages: + msg124802
stage: needs patch
2010-12-28 16:27:49michael.foordsetassignee: michael.foord
messages: + msg124801
nosy: michael.foord, cooyeah
2010-12-28 16:17:49r.david.murraysetkeywords: + easy
nosy: + michael.foord

versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6
2010-12-28 06:57:57cooyeahcreate