Skip to content

Commit 54c9f93

Browse files
committed
Added nicer test finder from Flask
1 parent 3b76f04 commit 54c9f93

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include MANIFEST.in Makefile CHANGES LICENSE AUTHORS
1+
include MANIFEST.in Makefile CHANGES LICENSE AUTHORS run-tests.py
22
recursive-include docs *
33
recursive-include custom_fixers *
44
recursive-include ext *

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test:
2-
python setup.py test
2+
python run-tests.py
33

44
develop:
55
pip install --editable .

jinja2/testsuite/__init__.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,57 @@ def assert_traceback_matches(self, callback, expected_tb):
6969
self.fail('Expected exception')
7070

7171

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._testMethodName
84+
)
85+
86+
87+
class BetterLoader(unittest.TestLoader):
88+
"""A nicer loader that solves two problems. First of all we are setting
89+
up tests from different sources and we're doing this programmatically
90+
which breaks the default loading logic so this is required anyways.
91+
Secondly this loader has a nicer interpolation for test names than the
92+
default one so you can just do ``run-tests.py ViewTestCase`` and it
93+
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 root
103+
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 rv
121+
122+
72123
def suite():
73124
from jinja2.testsuite import ext, filters, tests, core_tags, \
74125
loader, inheritance, imports, lexnparse, security, api, \
@@ -94,3 +145,11 @@ def suite():
94145
suite.addTest(doctests.suite())
95146

96147
return suite
148+
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)

run-tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
import sys, os
3+
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
4+
from jinja2.testsuite import main
5+
main()

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
envlist = py26, py27, pypy, py33
33

44
[testenv]
5-
commands = python setup.py test
5+
commands = python run-tests.py

0 commit comments

Comments
 (0)