Tests pass now.
This commit is contained in:
parent
961db8ad72
commit
8fa5e32d9a
24 changed files with 421 additions and 530 deletions
|
|
@ -11,16 +11,15 @@
|
|||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
import pytest
|
||||
|
||||
import os
|
||||
import sys
|
||||
import flask
|
||||
import warnings
|
||||
import unittest
|
||||
from functools import update_wrapper
|
||||
from contextlib import contextmanager
|
||||
from werkzeug.utils import import_string, find_modules
|
||||
from flask._compat import reraise, StringIO
|
||||
from flask._compat import StringIO
|
||||
|
||||
|
||||
def add_to_path(path):
|
||||
|
|
@ -43,29 +42,6 @@ def add_to_path(path):
|
|||
sys.path.insert(0, path)
|
||||
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def catch_warnings():
|
||||
"""Catch warnings in a with block in a list"""
|
||||
|
|
@ -76,6 +52,7 @@ def catch_warnings():
|
|||
warnings.filters = filters[:]
|
||||
old_showwarning = warnings.showwarning
|
||||
log = []
|
||||
|
||||
def showwarning(message, category, filename, lineno, file=None, line=None):
|
||||
log.append(locals())
|
||||
try:
|
||||
|
|
@ -107,12 +84,23 @@ def emits_module_deprecation_warning(f):
|
|||
return update_wrapper(new_f, f)
|
||||
|
||||
|
||||
class FlaskTestCase(unittest.TestCase):
|
||||
class TestFlask(object):
|
||||
"""Baseclass for all the tests that Flask uses. Use these methods
|
||||
for testing instead of the camelcased ones in the baseclass for
|
||||
consistency.
|
||||
"""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_path(self, monkeypatch):
|
||||
monkeypatch.syspath_prepend(
|
||||
os.path.abspath(os.path.join(
|
||||
os.path.dirname(__file__), 'test_apps'))
|
||||
)
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def leak_detector(self, request):
|
||||
request.addfinalizer(self.ensure_clean_request_context)
|
||||
|
||||
def ensure_clean_request_context(self):
|
||||
# make sure we're not leaking a request context since we are
|
||||
# testing flask internally in debug mode in a few cases
|
||||
|
|
@ -121,133 +109,42 @@ class FlaskTestCase(unittest.TestCase):
|
|||
leaks.append(flask._request_ctx_stack.pop())
|
||||
self.assert_equal(leaks, [])
|
||||
|
||||
def setup_method(self, method):
|
||||
self.setup()
|
||||
|
||||
def teardown_method(self, method):
|
||||
self.teardown()
|
||||
|
||||
def setup(self):
|
||||
pass
|
||||
|
||||
def teardown(self):
|
||||
pass
|
||||
|
||||
def setUp(self):
|
||||
self.setup()
|
||||
|
||||
def tearDown(self):
|
||||
unittest.TestCase.tearDown(self)
|
||||
self.ensure_clean_request_context()
|
||||
self.teardown()
|
||||
|
||||
def assert_equal(self, x, y):
|
||||
return self.assertEqual(x, y)
|
||||
assert 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)
|
||||
if callable:
|
||||
return pytest.raises(exc_type, callable, *args, **kwargs)
|
||||
else:
|
||||
return pytest.raises(exc_type)
|
||||
|
||||
def assert_true(self, x, msg=None):
|
||||
self.assertTrue(x, msg)
|
||||
assert x
|
||||
assert_ = assert_true
|
||||
|
||||
def assert_false(self, x, msg=None):
|
||||
self.assertFalse(x, msg)
|
||||
assert not x
|
||||
|
||||
def assert_in(self, x, y):
|
||||
self.assertIn(x, y)
|
||||
assert x in y
|
||||
|
||||
def assert_not_in(self, x, y):
|
||||
self.assertNotIn(x, y)
|
||||
assert x not in y
|
||||
|
||||
def assert_isinstance(self, obj, cls):
|
||||
self.assertIsInstance(obj, cls)
|
||||
assert isinstance(obj, cls)
|
||||
|
||||
if sys.version_info[:2] == (2, 6):
|
||||
def assertIn(self, x, y):
|
||||
assert x in y, "%r unexpectedly not in %r" % (x, y)
|
||||
|
||||
def assertNotIn(self, x, y):
|
||||
assert x not in y, "%r unexpectedly in %r" % (x, y)
|
||||
|
||||
def assertIsInstance(self, x, y):
|
||||
assert isinstance(x, y), "not isinstance(%r, %r)" % (x, y)
|
||||
|
||||
|
||||
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):
|
||||
reraise(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 setup_path():
|
||||
add_to_path(os.path.abspath(os.path.join(
|
||||
os.path.dirname(__file__), 'test_apps')))
|
||||
|
||||
|
||||
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.
|
||||
"""
|
||||
setup_path()
|
||||
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 as e:
|
||||
print('Error: %s' % e)
|
||||
def fail(self, msg):
|
||||
raise AssertionError(msg)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue