Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added nicer test finder from Flask

  • Loading branch information...
commit 54c9f93e1f6d2bb564a3ed0cf38189effb4e2698 1 parent 3b76f04
Armin Ronacher authored
2  MANIFEST.in
View
@@ -1,4 +1,4 @@
-include MANIFEST.in Makefile CHANGES LICENSE AUTHORS
+include MANIFEST.in Makefile CHANGES LICENSE AUTHORS run-tests.py
recursive-include docs *
recursive-include custom_fixers *
recursive-include ext *
2  Makefile
View
@@ -1,5 +1,5 @@
test:
- python setup.py test
+ python run-tests.py
develop:
pip install --editable .
59 jinja2/testsuite/__init__.py
View
@@ -69,6 +69,57 @@ def assert_traceback_matches(self, callback, expected_tb):
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)
5 run-tests.py
View
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+import sys, os
+sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
+from jinja2.testsuite import main
+main()
2  tox.ini
View
@@ -2,4 +2,4 @@
envlist = py26, py27, pypy, py33
[testenv]
-commands = python setup.py test
+commands = python run-tests.py
Please sign in to comment.
Something went wrong with that request. Please try again.