15 from __future__
import absolute_import
24 TEST_MODULE_REGEX =
r'^.*_test$'
28 """Test loader for setuptools test suite support.
31 suite (unittest.TestSuite): All tests collected by the loader.
32 loader (unittest.TestLoader): Standard Python unittest loader to be ran per
34 module_matcher (re.RegexObject): A regular expression object to match
35 against module names and determine whether or not the discovered module
36 contributes to the test suite.
40 self.
suite = unittest.TestSuite()
45 """Function mirroring TestLoader::loadTestsFromNames, as expected by
46 setuptools.setup argument `test_loader`."""
49 coverage_context = coverage.Coverage(data_suffix=
True)
50 coverage_context.start()
51 imported_modules = tuple(
52 importlib.import_module(name)
for name
in names)
53 for imported_module
in imported_modules:
55 for imported_module
in imported_modules:
57 package_paths = imported_module.__path__
58 except AttributeError:
61 coverage_context.stop()
62 coverage_context.save()
66 """Walks over the packages, dispatching `visit_module` calls.
69 package_paths (list): A list of paths over which to walk through modules
72 for importer, module_name, is_package
in (
73 pkgutil.walk_packages(package_paths)):
74 module = importer.find_module(module_name).load_module(module_name)
78 """Visits the module, adding discovered tests to the test suite.
81 module (module): Module to match against self.module_matcher; if matched
82 it has its tests loaded via self.loader into self.suite.
85 module_suite = self.
loader.loadTestsFromModule(module)
86 self.
suite.addTest(module_suite)
90 """Generator over all unittest.TestCases in a unittest.TestSuite.
93 suite (unittest.TestSuite): Suite to iterate over in the generator.
96 generator: A generator over all unittest.TestCases in `suite`.
99 if isinstance(item, unittest.TestSuite):
102 elif isinstance(item, unittest.TestCase):
105 raise ValueError(
'unexpected suite item of type {}'.
format(