Title: Fix test discovery for on Windows
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.3, Python 3.4
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: brett.cannon, ezio.melotti, python-dev, zach.ware
Priority: normal Keywords: patch

Created on 2013-02-16 23:02 by zach.ware, last changed 2013-02-23 05:59 by ezio.melotti. This issue is now closed.

File name Uploaded Description Edit
test_format_discovery.diff zach.ware, 2013-02-16 23:02 Possible solution 1 review
issue17217.diff ezio.melotti, 2013-02-20 18:42
Messages (4)
msg182248 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2013-02-16 23:02
test_format is an interesting case in the ongoing test discovery conversion.  It already uses unittest.main(), but still has test_main(), and discovery seems to work fine on Linux.  On Windows, however, test_format and test_non_ascii both fail with a UnicodeEncodeError.  Running under regrtest, this doesn't happen because of regrtest's replace_stdout() function.  Thus, running python -m test.test_format with the following patch works:

diff -r 168efd87e051 Lib/test/
--- a/Lib/test/   Fri Feb 15 23:38:23 2013 +0200
+++ b/Lib/test/   Sat Feb 16 15:14:52 2013 -0600
@@ -325,9 +325,7 @@
         self.assertIs("{0:5s}".format(text), text)

-def test_main():
-    support.run_unittest(FormatTest)
 if __name__ == "__main__":
+    from test.regrtest import replace_stdout
+    replace_stdout()

Doing the same in a setUpModule function would work for both running the module and for a unittest discover command, but it would also cause replace_stdout() to be called twice when running under regrtest, which I don't think would be especially good.

The attached patch is one possible solution that I've come up with.  It moves replace_stdout from regrtest to support, and adds a "running_under_regrtest" flag to support that regrtest sets to True.  test_format then checks support.running_under_regrtest in setUpModule to determine whether to run support.replace_stdout.
msg182541 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-02-20 18:42
In order to fix test discovery on Windows the attached patch should be enough.

There are two somewhat unrelated issues though:
1) moving replace_stdout to (and possibly turn it into a context manager/decorator);
2) use unittest verbosity to control the output of test_format instead of;

Apparently there's no API to access the unittest verbosity level, so that would be a new feature request.
msg182678 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2013-02-22 17:59
Your patch is certainly much simpler and more effective than mine; consider mine withdrawn.  I didn't take the simple step of figuring out exactly where the problem was happening and was just trying to fix the issue without changing anything in the actual tests....oops :-S

I'll raise other issues about moving replace_stdout and unittest verbosity soon.
msg182716 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-02-23 05:58
New changeset 831be7dc260a by Ezio Melotti in branch '3.2':
#17217: fix UnicodeEncodeErrors errors in test_format by printing ASCII only.

New changeset 3eb693462891 by Ezio Melotti in branch '3.3':
#17217: merge with 3.2.

New changeset 562ba95dd4c9 by Ezio Melotti in branch 'default':
#17217: merge with 3.3.
Date User Action Args
2013-02-23 05:59:23ezio.melottisetstatus: open -> closed
assignee: ezio.melotti
resolution: fixed
stage: resolved
2013-02-23 05:58:43python-devsetnosy: + python-dev
messages: + msg182716
2013-02-22 17:59:53zach.waresetmessages: + msg182678
2013-02-20 18:42:06ezio.melottisetfiles: + issue17217.diff

messages: + msg182541
2013-02-16 23:02:28zach.warecreate