Added nicer test finder from Flask
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 3b76f04 · Browse files · 5 files·+67 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+67 added−3 removed
- other:+67 added−3 removed
Provenance
- Commit
54c9f93- Tree
6fe2407- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
3b76f04
5 files changed+67 added−3 removed
modified:jinja2/testsuite/__init__.py+59 added
67 unmodified lines68 else:69 self.fail('Expected exception')70 71 72+def find_all_tests(suite):73+ """Yields all the tests and their names from a given suite."""74+ suites = [suite]75+ while suites:76+ s = suites.pop()77+ try:78+ suites.extend(s)79+ except TypeError:80+ yield s, '%s.%s.%s' % (81+ s.__class__.__module__,82+ s.__class__.__name__,83+ s._testMethodName84+ )85+86+87+class BetterLoader(unittest.TestLoader):88+ """A nicer loader that solves two problems. First of all we are setting89+ up tests from different sources and we're doing this programmatically90+ which breaks the default loading logic so this is required anyways.91+ Secondly this loader has a nicer interpolation for test names than the92+ default one so you can just do ``run-tests.py ViewTestCase`` and it93+ will work.94+ """95+96+ def getRootSuite(self):97+ return suite()98+99+ def loadTestsFromName(self, name, module=None):100+ root = self.getRootSuite()101+ if name == 'suite':102+ return root103+104+ all_tests = []105+ for testcase, testname in find_all_tests(root):106+ if testname == name or \107+ testname.endswith('.' + name) or \108+ ('.' + name + '.') in testname or \109+ testname.startswith(name + '.'):110+ all_tests.append(testcase)111+112+ if not all_tests:113+ raise LookupError('could not find test case for "%s"' % name)114+115+ if len(all_tests) == 1:116+ return all_tests[0]117+ rv = unittest.TestSuite()118+ for test in all_tests:119+ rv.addTest(test)120+ return rv121+122+123 def suite():124 from jinja2.testsuite import ext, filters, tests, core_tags, \125 loader, inheritance, imports, lexnparse, security, api, \126 regression, debug, utils, doctests17 unmodified lines144 if PY2:145 suite.addTest(doctests.suite())146 147 return suite148+149+150+def main():151+ """Runs the testsuite as command line application."""152+ try:153+ unittest.main(testLoader=BetterLoader(), defaultTest='suite')154+ except Exception as e:155+ print('Error: %s' % e)modified:Makefile+1 added-1 removed
1 test:2- python setup.py test2+ python run-tests.py3 4 develop:5 pip install --editable .6 15 unmodified linesmodified:MANIFEST.in+1 added-1 removed
1-include MANIFEST.in Makefile CHANGES LICENSE AUTHORS1+include MANIFEST.in Makefile CHANGES LICENSE AUTHORS run-tests.py2 recursive-include docs *3 recursive-include custom_fixers *4 recursive-include ext *5 recursive-include artwork *7 unmodified linesadded:run-tests.py+5 added
1+#!/usr/bin/env python2+import sys, os3+sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))4+from jinja2.testsuite import main5+main()modified:tox.ini+1 added-1 removed
1 [tox]2 envlist = py26, py27, pypy, py333 4 [testenv]5-commands = python setup.py test5+commands = python run-tests.py