OLD | NEW |
(Empty) | |
| 1 # Copyright 2015, Google Inc. |
| 2 # All rights reserved. |
| 3 # |
| 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are |
| 6 # met: |
| 7 # |
| 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above |
| 11 # copyright notice, this list of conditions and the following disclaimer |
| 12 # in the documentation and/or other materials provided with the |
| 13 # distribution. |
| 14 # * Neither the name of Google Inc. nor the names of its |
| 15 # contributors may be used to endorse or promote products derived from |
| 16 # this software without specific prior written permission. |
| 17 # |
| 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 import importlib |
| 31 import pkgutil |
| 32 import re |
| 33 import unittest |
| 34 |
| 35 import coverage |
| 36 |
| 37 TEST_MODULE_REGEX = r'^.*_test$' |
| 38 |
| 39 |
| 40 class Loader(object): |
| 41 """Test loader for setuptools test suite support. |
| 42 |
| 43 Attributes: |
| 44 suite (unittest.TestSuite): All tests collected by the loader. |
| 45 loader (unittest.TestLoader): Standard Python unittest loader to be ran per |
| 46 module discovered. |
| 47 module_matcher (re.RegexObject): A regular expression object to match |
| 48 against module names and determine whether or not the discovered module |
| 49 contributes to the test suite. |
| 50 """ |
| 51 |
| 52 def __init__(self): |
| 53 self.suite = unittest.TestSuite() |
| 54 self.loader = unittest.TestLoader() |
| 55 self.module_matcher = re.compile(TEST_MODULE_REGEX) |
| 56 |
| 57 def loadTestsFromNames(self, names, module=None): |
| 58 """Function mirroring TestLoader::loadTestsFromNames, as expected by |
| 59 setuptools.setup argument `test_loader`.""" |
| 60 # ensure that we capture decorators and definitions (else our coverage |
| 61 # measure unnecessarily suffers) |
| 62 coverage_context = coverage.Coverage(data_suffix=True) |
| 63 coverage_context.start() |
| 64 modules = [importlib.import_module(name) for name in names] |
| 65 for module in modules: |
| 66 self.visit_module(module) |
| 67 for module in modules: |
| 68 try: |
| 69 package_paths = module.__path__ |
| 70 except: |
| 71 continue |
| 72 self.walk_packages(package_paths) |
| 73 coverage_context.stop() |
| 74 coverage_context.save() |
| 75 return self.suite |
| 76 |
| 77 def walk_packages(self, package_paths): |
| 78 """Walks over the packages, dispatching `visit_module` calls. |
| 79 |
| 80 Args: |
| 81 package_paths (list): A list of paths over which to walk through modules |
| 82 along. |
| 83 """ |
| 84 for importer, module_name, is_package in ( |
| 85 pkgutil.iter_modules(package_paths)): |
| 86 module = importer.find_module(module_name).load_module(module_name) |
| 87 self.visit_module(module) |
| 88 if is_package: |
| 89 self.walk_packages(module.__path__) |
| 90 |
| 91 def visit_module(self, module): |
| 92 """Visits the module, adding discovered tests to the test suite. |
| 93 |
| 94 Args: |
| 95 module (module): Module to match against self.module_matcher; if matched |
| 96 it has its tests loaded via self.loader into self.suite. |
| 97 """ |
| 98 if self.module_matcher.match(module.__name__): |
| 99 module_suite = self.loader.loadTestsFromModule(module) |
| 100 self.suite.addTest(module_suite) |
| 101 |
| 102 |
| 103 def iterate_suite_cases(suite): |
| 104 """Generator over all unittest.TestCases in a unittest.TestSuite. |
| 105 |
| 106 Args: |
| 107 suite (unittest.TestSuite): Suite to iterate over in the generator. |
| 108 |
| 109 Returns: |
| 110 generator: A generator over all unittest.TestCases in `suite`. |
| 111 """ |
| 112 for item in suite: |
| 113 if isinstance(item, unittest.TestSuite): |
| 114 for child_item in iterate_suite_cases(item): |
| 115 yield child_item |
| 116 elif isinstance(item, unittest.TestCase): |
| 117 yield item |
| 118 else: |
| 119 raise ValueError('unexpected suite item of type {}'.format(type(item))) |
OLD | NEW |