Moved loader code into the testsuite and out of the runner
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 f30b117 · Browse files · 2 files·+53 added−56 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+53 added−56 removed
- other:+53 added−56 removed
Provenance
- Commit
5a49688- Tree
ff89e3c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
f30b117
2 files changed+53 added−56 removed
modified:flask/testsuite/__init__.py+52 added
18 unmodified lines19 from contextlib import contextmanager20 from werkzeug.utils import import_string, find_modules21 22 23+common_prefix = __name__ + '.'24+25+26 def add_to_path(path):27 def _samefile(x, y):28 try:29 return os.path.samefile(x, y)19 unmodified lines49 if hasattr(mod, 'suite'):50 yield mod.suite()51 52 53+def find_all_tests():54+ suites = [suite()]55+ while suites:56+ s = suites.pop()57+ try:58+ suites.extend(s)59+ except TypeError:60+ yield s61+62+63+def find_all_tests_with_name():64+ for testcase in find_all_tests():65+ yield testcase, '%s.%s.%s' % (66+ testcase.__class__.__module__,67+ testcase.__class__.__name__,68+ testcase._testMethodName69+ )70+71+72 @contextmanager73 def catch_warnings():74 """Catch warnings in a with block in a list"""75 # make sure deprecation warnings are active in tests58 unmodified lines134 def assert_equal(self, x, y):135 return self.assertEqual(x, y)136 137 138+class BetterLoader(unittest.TestLoader):139+140+ def loadTestsFromName(self, name, module=None):141+ if name == 'suite':142+ return suite()143+ for testcase, testname in find_all_tests_with_name():144+ if testname == name:145+ return testcase146+ if testname.startswith(common_prefix):147+ if testname[len(common_prefix):] == name:148+ return testcase149+150+ all_tests = []151+ for testcase, testname in find_all_tests_with_name():152+ if testname.endswith('.' + name) or ('.' + name + '.') in testname or \153+ testname.startswith(name + '.'):154+ all_tests.append(testcase)155+156+ if not all_tests:157+ print >> sys.stderr, 'Error: could not find test case for "%s"' % name158+ sys.exit(1)159+160+ if len(all_tests) == 1:161+ return all_tests[0]162+ rv = unittest.TestSuite()163+ for test in all_tests:164+ rv.addTest(test)165+ return rv166+167+168 def suite():169 setup_paths()170 suite = unittest.TestSuite()171 for other_suite in iter_suites():2 unmodified linesmodified:run-tests.py+1 added-56 removed
1-import sys1 import unittest3-from unittest.loader import TestLoader4-from flask.testsuite import suite5-6-common_prefix = suite.__module__ + '.'7-8-9-def find_all_tests():10- suites = [suite()]11- while suites:12- s = suites.pop()13- try:14- suites.extend(s)15- except TypeError:16- yield s17-18-19-def find_all_tests_with_name():20- for testcase in find_all_tests():21- yield testcase, '%s.%s.%s' % (22- testcase.__class__.__module__,23- testcase.__class__.__name__,24- testcase._testMethodName25- )26-27-28-class BetterLoader(TestLoader):29-30- def loadTestsFromName(self, name, module=None):31- if name == 'suite':32- return suite()33- for testcase, testname in find_all_tests_with_name():34- if testname == name:35- return testcase36- if testname.startswith(common_prefix):37- if testname[len(common_prefix):] == name:38- return testcase39-40- all_tests = []41- for testcase, testname in find_all_tests_with_name():42- if testname.endswith('.' + name) or ('.' + name + '.') in testname or \43- testname.startswith(name + '.'):44- all_tests.append(testcase)45-46- if not all_tests:47- print >> sys.stderr, 'Error: could not find test case for "%s"' % name48- sys.exit(1)49-50- if len(all_tests) == 1:51- return all_tests[0]52- rv = unittest.TestSuite()53- for test in all_tests:54- rv.addTest(test)55- return rv56-57-2+from flask.testsuite import BetterLoader3 unittest.main(testLoader=BetterLoader(), defaultTest='suite')