From 4e91f6720b97ad8c294552b88410b0ed689c0768 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Thu, 30 Aug 2012 17:43:54 -0400 Subject: [PATCH] Start using new unittest features from py27/unittest2. Tornado's test suite now requires unittest2 on py25 and py26. tornado.testing will use unittest2 if available but does not require it. This means we can no longer run the test suite under app engine 2.5, although it still works for app engine 2.7. --- maint/test/appengine/tox.ini | 6 +++++- tornado/test/escape_test.py | 2 +- tornado/test/httpserver_test.py | 2 +- tornado/test/httputil_test.py | 2 +- tornado/test/import_test.py | 2 +- tornado/test/ioloop_test.py | 2 +- tornado/test/locale_test.py | 2 +- tornado/test/options_test.py | 2 +- tornado/test/runtests.py | 2 +- tornado/test/stack_context_test.py | 2 +- tornado/test/testing_test.py | 2 +- tornado/test/twisted_test.py | 2 +- tornado/test/util.py | 10 ++++++++++ tornado/test/util_test.py | 4 ++-- tornado/testing.py | 10 +++++++++- tox.ini | 10 +++++++++- 16 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 tornado/test/util.py diff --git a/maint/test/appengine/tox.ini b/maint/test/appengine/tox.ini index 0970bf88..dcc7ac2f 100644 --- a/maint/test/appengine/tox.ini +++ b/maint/test/appengine/tox.ini @@ -5,14 +5,18 @@ # These are currently excluded from the main tox.ini because their # logs are spammy and they're a little flaky. [tox] -envlist = py25-appengine, py27-appengine +envlist = py27-appengine [testenv] changedir = {toxworkdir} +# py25-appengine was broken by the addition of unittest2. Need to figure +# out a way to smuggle unittest2 into the appengine environment if we don't +# drop py25 support. [testenv:py25-appengine] basepython = python2.5 commands = python {toxinidir}/py25/runtests.py {posargs:} +deps = unittest2 [testenv:py27-appengine] basepython = python2.7 diff --git a/tornado/test/escape_test.py b/tornado/test/escape_test.py index defca1ba..793e3c8e 100644 --- a/tornado/test/escape_test.py +++ b/tornado/test/escape_test.py @@ -3,10 +3,10 @@ from __future__ import absolute_import, division, with_statement import tornado.escape -import unittest from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode from tornado.util import b +from tornado.test.util import unittest linkify_tests = [ # (input, linkify_kwargs, expected_output) diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 190f1abc..41572715 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -10,6 +10,7 @@ from tornado.ioloop import IOLoop from tornado.iostream import IOStream from tornado.simple_httpclient import SimpleAsyncHTTPClient from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, LogTrapTestCase +from tornado.test.util import unittest from tornado.util import b, bytes_type from tornado.web import Application, RequestHandler import os @@ -17,7 +18,6 @@ import shutil import socket import sys import tempfile -import unittest try: import ssl diff --git a/tornado/test/httputil_test.py b/tornado/test/httputil_test.py index 736ed6d6..f1319cde 100644 --- a/tornado/test/httputil_test.py +++ b/tornado/test/httputil_test.py @@ -5,9 +5,9 @@ from __future__ import absolute_import, division, with_statement from tornado.httputil import url_concat, parse_multipart_form_data, HTTPHeaders from tornado.escape import utf8 from tornado.testing import LogTrapTestCase +from tornado.test.util import unittest from tornado.util import b import logging -import unittest class TestUrlConcat(unittest.TestCase): diff --git a/tornado/test/import_test.py b/tornado/test/import_test.py index 584f070d..97c2277b 100644 --- a/tornado/test/import_test.py +++ b/tornado/test/import_test.py @@ -1,5 +1,5 @@ from __future__ import absolute_import, division, with_statement -import unittest +from tornado.test.util import unittest class ImportTest(unittest.TestCase): diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index 2ebdbcea..4e5e123c 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -5,11 +5,11 @@ from __future__ import absolute_import, division, with_statement import datetime import socket import time -import unittest from tornado.ioloop import IOLoop from tornado.netutil import bind_sockets from tornado.testing import AsyncTestCase, LogTrapTestCase, get_unused_port +from tornado.test.util import unittest class TestIOLoop(AsyncTestCase, LogTrapTestCase): diff --git a/tornado/test/locale_test.py b/tornado/test/locale_test.py index 586080b1..4131dd0f 100644 --- a/tornado/test/locale_test.py +++ b/tornado/test/locale_test.py @@ -2,7 +2,7 @@ from __future__ import absolute_import, division, with_statement import os import tornado.locale -import unittest +from tornado.test.util import unittest class TranslationLoaderTest(unittest.TestCase): diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py index 72be4459..c815935c 100644 --- a/tornado/test/options_test.py +++ b/tornado/test/options_test.py @@ -4,11 +4,11 @@ import logging import os import re import tempfile -import unittest import warnings from tornado.escape import utf8 from tornado.options import _Options, _LogFormatter +from tornado.test.util import unittest from tornado.util import b, bytes_type diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index eea052ea..29c82d4d 100644 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -2,7 +2,7 @@ from __future__ import absolute_import, division, with_statement import sys -import unittest +from tornado.test.util import unittest TEST_MODULES = [ 'tornado.httputil.doctests', diff --git a/tornado/test/stack_context_test.py b/tornado/test/stack_context_test.py index dc717596..d911d169 100644 --- a/tornado/test/stack_context_test.py +++ b/tornado/test/stack_context_test.py @@ -3,12 +3,12 @@ from __future__ import absolute_import, division, with_statement from tornado.stack_context import StackContext, wrap from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, LogTrapTestCase +from tornado.test.util import unittest from tornado.util import b from tornado.web import asynchronous, Application, RequestHandler import contextlib import functools import logging -import unittest class TestRequestHandler(RequestHandler): diff --git a/tornado/test/testing_test.py b/tornado/test/testing_test.py index 1de20df1..77f55e7e 100644 --- a/tornado/test/testing_test.py +++ b/tornado/test/testing_test.py @@ -1,9 +1,9 @@ #!/usr/bin/env python from __future__ import absolute_import, division, with_statement -import unittest import time from tornado.testing import AsyncTestCase, LogTrapTestCase +from tornado.test.util import unittest class AsyncTestCaseTest(AsyncTestCase, LogTrapTestCase): diff --git a/tornado/test/twisted_test.py b/tornado/test/twisted_test.py index cd7fc60d..3178f94d 100644 --- a/tornado/test/twisted_test.py +++ b/tornado/test/twisted_test.py @@ -25,7 +25,6 @@ import signal import tempfile import thread import threading -import unittest try: import fcntl @@ -46,6 +45,7 @@ from tornado.httpclient import AsyncHTTPClient from tornado.ioloop import IOLoop from tornado.platform.auto import set_close_exec from tornado.testing import get_unused_port +from tornado.test.util import unittest from tornado.util import import_object from tornado.web import RequestHandler, Application diff --git a/tornado/test/util.py b/tornado/test/util.py new file mode 100644 index 00000000..26d843d5 --- /dev/null +++ b/tornado/test/util.py @@ -0,0 +1,10 @@ +from __future__ import absolute_import, division, with_statement + +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: + import unittest2 as unittest diff --git a/tornado/test/util_test.py b/tornado/test/util_test.py index 8707b0d5..338c16a9 100644 --- a/tornado/test/util_test.py +++ b/tornado/test/util_test.py @@ -1,8 +1,8 @@ from __future__ import absolute_import, division, with_statement import sys -import unittest from tornado.util import raise_exc_info +from tornado.test.util import unittest class RaiseExcInfoTest(unittest.TestCase): @@ -23,4 +23,4 @@ class RaiseExcInfoTest(unittest.TestCase): raise_exc_info(exc_info) self.fail("didn't get expected exception") except TwoArgException, e: - self.assertTrue(e is exc_info[1]) + self.assertIs(e, exc_info[1]) diff --git a/tornado/testing.py b/tornado/testing.py index c18f17be..c597e3a7 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -41,7 +41,15 @@ import os import signal import sys import time -import unittest + +# Tornado's own test suite requires the updated unittest module +# (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: + import unittest _next_port = 10000 diff --git a/tox.ini b/tox.ini index e072d119..f4291e38 100644 --- a/tox.ini +++ b/tox.ini @@ -28,7 +28,9 @@ changedir = {toxworkdir} [testenv:py25] basepython = python2.5 -deps = simplejson +deps = + simplejson + unittest2 [testenv:py25-full] basepython = python2.5 @@ -38,9 +40,14 @@ deps = simplejson # twisted is dropping python 2.5 support in 12.2.0 twisted<=12.1.0 + unittest2 # zope.interface (used by twisted) dropped python 2.5 support in 4.0 zope.interface<4.0 +[testenv:py26] +basepython = python2.6 +deps = unittest2 + # py26-full deliberately runs an older version of twisted to ensure # we're still compatible with the oldest version we support. [testenv:py26-full] @@ -49,6 +56,7 @@ deps = MySQL-python pycurl twisted==11.0.0 + unittest2 [testenv:py27-full] basepython = python2.7