Skip to content
Myr

Cleanup in the test finder

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 5a49688 · Browse files · 1 file+27 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+27 added−24 removed

  • other:+27 added−24 removed

1 file+27 added−24 removed

modified:flask/testsuite/__init__.py+27 added-24 removed
18 unmodified lines
19from contextlib import contextmanager20from werkzeug.utils import import_string, find_modules212223common_prefix = __name__ + '.'242523def add_to_path(path):24 def _samefile(x, y):25 try:26 return os.path.samefile(x, y)
19 unmodified lines
46 if hasattr(mod, 'suite'):47 yield mod.suite()484953def find_all_tests():54 suites = [suite()]50def find_all_tests(suite):51 suites = [suite]52 while suites:53 s = suites.pop()54 try:55 suites.extend(s)56 except TypeError:60 yield s57 yield s, '%s.%s.%s' % (58 s.__class__.__module__,59 s.__class__.__name__,60 s._testMethodName61 )626363def 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 )707164@contextmanager65def catch_warnings():66 """Catch warnings in a with block in a list"""67 # make sure deprecation warnings are active in tests
34 unmodified lines
102 return update_wrapper(new_f, f)103104105class FlaskTestCase(unittest.TestCase):106 """Baseclass for all the tests that Flask uses. Use these methods107 for testing instead of the camelcased ones in the baseclass for108 consistency.109 """110111 def ensure_clean_request_context(self):112 # make sure we're not leaking a request context since we are113 # testing flask internally in debug mode in a few cases
17 unmodified lines
131 return self.assertEqual(x, y)132133134class BetterLoader(unittest.TestLoader):135 """A nicer loader that solves two problems. First of all we are setting136 up tests from different sources and we're doing this programmatically137 which breaks the default loading logic so this is required anyways.138 Secondly this loader has a nicer interpolation for test names than the139 default one so you can just do ``run-tests.py ViewTestCase`` and it140 will work.141 """142143 def getRootSuite(self):144 return suite()145146 def loadTestsFromName(self, name, module=None):147 root = self.getRootSuite()148 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 return root150151 all_tests = []151 for testcase, testname in find_all_tests_with_name():152 if testname.endswith('.' + name) or ('.' + name + '.') in testname or \152 for testcase, testname in find_all_tests(root):153 if testname == name or \154 testname.endswith('.' + name) or \155 ('.' + name + '.') in testname or \156 testname.startswith(name + '.'):157 all_tests.append(testcase)158159 if not all_tests:
17 unmodified lines