Skip to content
Myr

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

2 files+53 added−56 removed

modified:flask/testsuite/__init__.py+52 added
18 unmodified lines
19from contextlib import contextmanager20from werkzeug.utils import import_string, find_modules212223common_prefix = __name__ + '.'242526def add_to_path(path):27 def _samefile(x, y):28 try:29 return os.path.samefile(x, y)
19 unmodified lines
49 if hasattr(mod, 'suite'):50 yield mod.suite()515253def find_all_tests():54 suites = [suite()]55 while suites:56 s = suites.pop()57 try:58 suites.extend(s)59 except TypeError:60 yield s616263def 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 )707172@contextmanager73def catch_warnings():74 """Catch warnings in a with block in a list"""75 # make sure deprecation warnings are active in tests
58 unmodified lines
134 def assert_equal(self, x, y):135 return self.assertEqual(x, y)136137138class BetterLoader(unittest.TestLoader):139140 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 testcase149150 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)155156 if not all_tests:157 print >> sys.stderr, 'Error: could not find test case for "%s"' % name158 sys.exit(1)159160 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 rv166167168def suite():169 setup_paths()170 suite = unittest.TestSuite()171 for other_suite in iter_suites():
2 unmodified lines
modified:run-tests.py+1 added-56 removed
1import sys1import unittest3from unittest.loader import TestLoader4from flask.testsuite import suite56common_prefix = suite.__module__ + '.'789def find_all_tests():10 suites = [suite()]11 while suites:12 s = suites.pop()13 try:14 suites.extend(s)15 except TypeError:16 yield s171819def 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 )262728class BetterLoader(TestLoader):2930 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 testcase3940 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)4546 if not all_tests:47 print >> sys.stderr, 'Error: could not find test case for "%s"' % name48 sys.exit(1)4950 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 rv56572from flask.testsuite import BetterLoader3unittest.main(testLoader=BetterLoader(), defaultTest='suite')