From 54c9f93e1f6d2bb564a3ed0cf38189effb4e2698 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 20 May 2013 11:51:37 +0100 Subject: Added nicer test finder from Flask --- MANIFEST.in | 2 +- Makefile | 2 +- jinja2/testsuite/__init__.py | 59 ++++++++++++++++++++++++++++++++++++++++++++ run-tests.py | 5 ++++ tox.ini | 2 +- 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 run-tests.py diff --git a/MANIFEST.in b/MANIFEST.in index 63760bb..0d6db7c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -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 * diff --git a/Makefile b/Makefile index 9a31598..266180e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ test: - python setup.py test + python run-tests.py develop: pip install --editable . 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) diff --git a/run-tests.py b/run-tests.py new file mode 100644 index 0000000..f8d5024 --- /dev/null +++ b/run-tests.py @@ -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() diff --git a/tox.ini b/tox.ini index ffd4b87..4bad758 100644 --- a/tox.ini +++ b/tox.ini @@ -2,4 +2,4 @@ envlist = py26, py27, pypy, py33 [testenv] -commands = python setup.py test +commands = python run-tests.py -- cgit v1.2.1