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
Provenance
- Commit
a082a5e- Tree
2200999- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5a49688
1 file changed+27 added−24 removed
modified:flask/testsuite/__init__.py+27 added-24 removed
18 unmodified lines19 from contextlib import contextmanager20 from werkzeug.utils import import_string, find_modules21 22 23-common_prefix = __name__ + '.'24-25-23 def add_to_path(path):24 def _samefile(x, y):25 try:26 return os.path.samefile(x, y)19 unmodified lines46 if hasattr(mod, 'suite'):47 yield mod.suite()48 49 53-def find_all_tests():54- suites = [suite()]50+def 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+ )62 63 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-64 @contextmanager65 def catch_warnings():66 """Catch warnings in a with block in a list"""67 # make sure deprecation warnings are active in tests34 unmodified lines102 return update_wrapper(new_f, f)103 104 105 class 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+ """110 111 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 cases17 unmodified lines131 return self.assertEqual(x, y)132 133 134 class 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+ """142 143+ def getRootSuite(self):144+ return suite()145+146 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 root150 151 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)158 159 if not all_tests:17 unmodified lines