Skip to content

Commit

Permalink
Started work on the new testsuite
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Sep 8, 2011
1 parent b4d8a50 commit 0dd202f
Show file tree
Hide file tree
Showing 6 changed files with 1,008 additions and 0 deletions.
3 changes: 3 additions & 0 deletions run-tests.py
@@ -0,0 +1,3 @@
#!/usr/bin/env python
from werkzeug.testsuite import main
main()
146 changes: 146 additions & 0 deletions werkzeug/testsuite/__init__.py
@@ -0,0 +1,146 @@
# -*- coding: utf-8 -*-
"""
werkzeug.testsuite
~~~~~~~~~~~~~~~~~~
Contains all test Werkzeug tests.
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""

from __future__ import with_statement

import unittest
from werkzeug.utils import import_string, find_modules


def iter_suites():
"""Yields all testsuites."""
for module in find_modules(__name__):
mod = import_string(module)
if hasattr(mod, 'suite'):
yield mod.suite()


def find_all_tests(suite):
"""Yields all the tests and their names from a given suite."""
suites = [suite]
while suites:
s = suites.pop()
try:
suites.extend(s)
except TypeError:
yield s, '%s.%s.%s' % (
s.__class__.__module__,
s.__class__.__name__,
s._testMethodName
)


class WerkzeugTestCase(unittest.TestCase):
"""Baseclass for all the tests that Werkzeug uses. Use these
methods for testing instead of the camelcased ones in the
baseclass for consistency.
"""

def setup(self):
pass

def teardown(self):
pass

def setUp(self):
self.setup()

def tearDown(self):
unittest.TestCase.tearDown(self)
self.teardown()

def assert_equal(self, x, y):
return self.assertEqual(x, y)

def assert_not_equal(self, x, y):
return self.assertNotEqual(x, y)

def assert_raises(self, exc_type, callable=None, *args, **kwargs):
catcher = _ExceptionCatcher(self, exc_type)
if callable is None:
return catcher
with catcher:
callable(*args, **kwargs)


class _ExceptionCatcher(object):

def __init__(self, test_case, exc_type):
self.test_case = test_case
self.exc_type = exc_type

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, tb):
exception_name = self.exc_type.__name__
if exc_type is None:
self.test_case.fail('Expected exception of type %r' %
exception_name)
elif not issubclass(exc_type, self.exc_type):
raise exc_type, exc_value, tb
return True


class BetterLoader(unittest.TestLoader):
"""A nicer loader that solves two problems. First of all we are setting
up tests from different sources and we're doing this programmatically
which breaks the default loading logic so this is required anyways.
Secondly this loader has a nicer interpolation for test names than the
default one so you can just do ``run-tests.py ViewTestCase`` and it
will work.
"""

def getRootSuite(self):
return suite()

def loadTestsFromName(self, name, module=None):
root = self.getRootSuite()
if name == 'suite':
return root

all_tests = []
for testcase, testname in find_all_tests(root):
if testname == name or \
testname.endswith('.' + name) or \
('.' + name + '.') in testname or \
testname.startswith(name + '.'):
all_tests.append(testcase)

if not all_tests:
raise LookupError('could not find test case for "%s"' % name)

if len(all_tests) == 1:
return all_tests[0]
rv = unittest.TestSuite()
for test in all_tests:
rv.addTest(test)
return rv


def suite():
"""A testsuite that has all the Flask tests. You can use this
function to integrate the Flask tests into your own testsuite
in case you want to test that monkeypatches to Flask do not
break it.
"""
suite = unittest.TestSuite()
for other_suite in iter_suites():
suite.addTest(other_suite)
return suite


def main():
"""Runs the testsuite as command line application."""
try:
unittest.main(testLoader=BetterLoader(), defaultTest='suite')
except Exception, e:
print 'Error: %s' % e
58 changes: 58 additions & 0 deletions werkzeug/testsuite/compat.py
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
"""
werkzeug.testsuite.compat
~~~~~~~~~~~~~~~~~~~~~~~~~
Ensure that old stuff does not break on update.
:copyright: (c) 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import unittest
import warnings
from werkzeug.testsuite import WerkzeugTestCase

from werkzeug.wrappers import Response
from werkzeug.test import create_environ


class CompatTestCase(WerkzeugTestCase):

def test_old_imports(self):
from werkzeug.utils import Headers, MultiDict, CombinedMultiDict, \
Headers, EnvironHeaders
from werkzeug.http import Accept, MIMEAccept, CharsetAccept, \
LanguageAccept, ETags, HeaderSet, WWWAuthenticate, \
Authorization

def test_exposed_werkzeug_mod(self):
import werkzeug
for key in werkzeug.__all__:
# deprecated, skip it
if key in ('templates', 'Template'):
continue
getattr(werkzeug, key)

def test_fix_headers_in_response(self):
# ignore some warnings werkzeug emits for backwards compat
for msg in ['called into deprecated fix_headers',
'fix_headers changed behavior']:
warnings.filterwarnings('ignore', message=msg,
category=DeprecationWarning)

class MyResponse(Response):
def fix_headers(self, environ):
Response.fix_headers(self, environ)
self.headers['x-foo'] = "meh"
myresp = MyResponse('Foo')
resp = Response.from_app(myresp, create_environ(method='GET'))
assert resp.headers['x-foo'] == 'meh'
assert resp.data == 'Foo'

warnings.resetwarnings()


def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(CompatTestCase))
return suite

0 comments on commit 0dd202f

Please sign in to comment.