]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Refactor unittest/unittest2 imports to fix issues with unittest2 on py3.
authorBen Darnell <ben@bendarnell.com>
Sat, 15 Mar 2014 04:45:09 +0000 (00:45 -0400)
committerBen Darnell <ben@bendarnell.com>
Sat, 15 Mar 2014 04:46:52 +0000 (00:46 -0400)
Never use unittest2 on python 3 (it appears to not interoperate with
the standard unittest module).  On python 2, use the same logic in
tornado.testing and tornado.test.util to select an implementation.

Fixes #1005.

tornado/test/util.py
tornado/testing.py
tox.ini

index 36043104030b0b4cbe0fbd6b2846dd870e8e71fe..6a0fe80734a68f0a3a662975d47f1cac20d1ff1d 100644 (file)
@@ -5,10 +5,13 @@ import sys
 
 # Encapsulate the choice of unittest or unittest2 here.
 # To be used as 'from tornado.test.util import unittest'.
-if sys.version_info >= (2, 7):
-    import unittest
-else:
+if sys.version_info < (2, 7):
+    # In py26, we must always use unittest2.
     import unittest2 as unittest
+else:
+    # Otherwise, use whichever version of unittest was imported in
+    # tornado.testing.
+    from tornado.testing import unittest
 
 skipIfNonUnix = unittest.skipIf(os.name != 'posix' or sys.platform == 'cygwin',
                                 "non-unix platform")
index 96fdd32b0f4877483c8b24b94176a16a35489f0a..647f349c113ace2d5ee94eb86b25c06d92adee89 100644 (file)
@@ -49,10 +49,16 @@ except ImportError:
 # (either py27+ or unittest2) so tornado.test.util enforces
 # this requirement, but for other users of tornado.testing we want
 # to allow the older version if unitest2 is not available.
-try:
-    import unittest2 as unittest
-except ImportError:
+if sys.version_info >= (3,):
+    # On python 3, mixing unittest2 and unittest (including doctest)
+    # doesn't seem to work, so always use unittest.
     import unittest
+else:
+    # On python 2, prefer unittest2 when available.
+    try:
+        import unittest2 as unittest
+    except ImportError:
+        import unittest
 
 _next_port = 10000
 
diff --git a/tox.ini b/tox.ini
index 9f7cf379740e1a9688efd68457eb7cb06eb4e36a..3d67726c2b37a46d8e9204fd12ea69828a7b5276 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -36,6 +36,7 @@ envlist =
         py2-opt, py3-opt,
         py3-utf8,
         py2-locale,
+        py27-unittest2, py33-unittest2,
 
         # Ensure the sphinx build has no errors or warnings
         py2-docs
@@ -78,6 +79,12 @@ deps =
      pycurl
      twisted>=11.0.0
 
+# unittest2 doesn't add anything we need on 2.7, but we should ensure that
+# its existence doesn't break anything due to conditional imports.
+[testenv:py27-unittest2]
+basepython = python2.7
+deps = unittest2
+
 # In python 3, opening files in text mode uses a system-dependent encoding by
 # default.  Run the tests with "C" (ascii) and "utf-8" locales to ensure
 # we don't have hidden dependencies on this setting.
@@ -104,6 +111,12 @@ setenv = TORNADO_EXTENSION=1
 deps =
      pycurl>=7.19.3
 
+# See comment on py27-unittest2.
+[testenv:py33-unittest2]
+basepython = python3.3
+setenv = TORNADO_EXTENSION=1
+deps = unittest2py3k
+
 [testenv:py34-full]
 basepython = python3.4
 setenv = TORNADO_EXTENSION=1