summaryrefslogtreecommitdiff
path: root/jinja2
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-05-20 11:51:37 +0100
committerArmin Ronacher <armin.ronacher@active-4.com>2013-05-20 11:51:37 +0100
commit54c9f93e1f6d2bb564a3ed0cf38189effb4e2698 (patch)
tree6fe24070c54c24e33f44bd61a69b05153d729fc2 /jinja2
parent3b76f0441d3ea1d84a1f09ad8974ab180703ec4d (diff)
downloadjinja2-54c9f93e1f6d2bb564a3ed0cf38189effb4e2698.tar.gz
Added nicer test finder from Flask
Diffstat (limited to 'jinja2')
-rw-r--r--jinja2/testsuite/__init__.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/jinja2/testsuite/__init__.py b/jinja2/testsuite/__init__.py
index 781a0e7..f5e617b 100644
--- a/jinja2/testsuite/__init__.py
+++ b/jinja2/testsuite/__init__.py
@@ -69,6 +69,57 @@ class JinjaTestCase(unittest.TestCase):
self.fail('Expected exception')
+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 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():
from jinja2.testsuite import ext, filters, tests, core_tags, \
loader, inheritance, imports, lexnparse, security, api, \
@@ -94,3 +145,11 @@ def suite():
suite.addTest(doctests.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)