ยปCore Development>Code coverage>Lib/unittest/loader.py

Python code coverage for Lib/unittest/loader.py

#countcontent
1n/a"""Loading unittests."""
2n/a
3n/aimport os
4n/aimport re
5n/aimport sys
6n/aimport traceback
7n/aimport types
8n/aimport functools
9n/aimport warnings
10n/a
11n/afrom fnmatch import fnmatch
12n/a
13n/afrom . import case, suite, util
14n/a
15n/a__unittest = True
16n/a
17n/a# what about .pyc (etc)
18n/a# we would need to avoid loading the same tests multiple times
19n/a# from '.py', *and* '.pyc'
20n/aVALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
21n/a
22n/a
23n/aclass _FailedTest(case.TestCase):
24n/a _testMethodName = None
25n/a
26n/a def __init__(self, method_name, exception):
27n/a self._exception = exception
28n/a super(_FailedTest, self).__init__(method_name)
29n/a
30n/a def __getattr__(self, name):
31n/a if name != self._testMethodName:
32n/a return super(_FailedTest, self).__getattr__(name)
33n/a def testFailure():
34n/a raise self._exception
35n/a return testFailure
36n/a
37n/a
38n/adef _make_failed_import_test(name, suiteClass):
39n/a message = 'Failed to import test module: %s\n%s' % (
40n/a name, traceback.format_exc())
41n/a return _make_failed_test(name, ImportError(message), suiteClass, message)
42n/a
43n/adef _make_failed_load_tests(name, exception, suiteClass):
44n/a message = 'Failed to call load_tests:\n%s' % (traceback.format_exc(),)
45n/a return _make_failed_test(
46n/a name, exception, suiteClass, message)
47n/a
48n/adef _make_failed_test(methodname, exception, suiteClass, message):
49n/a test = _FailedTest(methodname, exception)
50n/a return suiteClass((test,)), message
51n/a
52n/adef _make_skipped_test(methodname, exception, suiteClass):
53n/a @case.skip(str(exception))
54n/a def testSkipped(self):
55n/a pass
56n/a attrs = {methodname: testSkipped}
57n/a TestClass = type("ModuleSkipped", (case.TestCase,), attrs)
58n/a return suiteClass((TestClass(methodname),))
59n/a
60n/adef _jython_aware_splitext(path):
61n/a if path.lower().endswith('$py.class'):
62n/a return path[:-9]
63n/a return os.path.splitext(path)[0]
64n/a
65n/a
66n/aclass TestLoader(object):
67n/a """
68n/a This class is responsible for loading tests according to various criteria
69n/a and returning them wrapped in a TestSuite
70n/a """
71n/a testMethodPrefix = 'test'
72n/a sortTestMethodsUsing = staticmethod(util.three_way_cmp)
73n/a suiteClass = suite.TestSuite
74n/a _top_level_dir = None
75n/a
76n/a def __init__(self):
77n/a super(TestLoader, self).__init__()
78n/a self.errors = []
79n/a # Tracks packages which we have called into via load_tests, to
80n/a # avoid infinite re-entrancy.
81n/a self._loading_packages = set()
82n/a
83n/a def loadTestsFromTestCase(self, testCaseClass):
84n/a """Return a suite of all test cases contained in testCaseClass"""
85n/a if issubclass(testCaseClass, suite.TestSuite):
86n/a raise TypeError("Test cases should not be derived from "
87n/a "TestSuite. Maybe you meant to derive from "
88n/a "TestCase?")
89n/a testCaseNames = self.getTestCaseNames(testCaseClass)
90n/a if not testCaseNames and hasattr(testCaseClass, 'runTest'):
91n/a testCaseNames = ['runTest']
92n/a loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
93n/a return loaded_suite
94n/a
95n/a # XXX After Python 3.5, remove backward compatibility hacks for
96n/a # use_load_tests deprecation via *args and **kws. See issue 16662.
97n/a def loadTestsFromModule(self, module, *args, pattern=None, **kws):
98n/a """Return a suite of all test cases contained in the given module"""
99n/a # This method used to take an undocumented and unofficial
100n/a # use_load_tests argument. For backward compatibility, we still
101n/a # accept the argument (which can also be the first position) but we
102n/a # ignore it and issue a deprecation warning if it's present.
103n/a if len(args) > 0 or 'use_load_tests' in kws:
104n/a warnings.warn('use_load_tests is deprecated and ignored',
105n/a DeprecationWarning)
106n/a kws.pop('use_load_tests', None)
107n/a if len(args) > 1:
108n/a # Complain about the number of arguments, but don't forget the
109n/a # required `module` argument.
110n/a complaint = len(args) + 1
111n/a raise TypeError('loadTestsFromModule() takes 1 positional argument but {} were given'.format(complaint))
112n/a if len(kws) != 0:
113n/a # Since the keyword arguments are unsorted (see PEP 468), just
114n/a # pick the alphabetically sorted first argument to complain about,
115n/a # if multiple were given. At least the error message will be
116n/a # predictable.
117n/a complaint = sorted(kws)[0]
118n/a raise TypeError("loadTestsFromModule() got an unexpected keyword argument '{}'".format(complaint))
119n/a tests = []
120n/a for name in dir(module):
121n/a obj = getattr(module, name)
122n/a if isinstance(obj, type) and issubclass(obj, case.TestCase):
123n/a tests.append(self.loadTestsFromTestCase(obj))
124n/a
125n/a load_tests = getattr(module, 'load_tests', None)
126n/a tests = self.suiteClass(tests)
127n/a if load_tests is not None:
128n/a try:
129n/a return load_tests(self, tests, pattern)
130n/a except Exception as e:
131n/a error_case, error_message = _make_failed_load_tests(
132n/a module.__name__, e, self.suiteClass)
133n/a self.errors.append(error_message)
134n/a return error_case
135n/a return tests
136n/a
137n/a def loadTestsFromName(self, name, module=None):
138n/a """Return a suite of all test cases given a string specifier.
139n/a
140n/a The name may resolve either to a module, a test case class, a
141n/a test method within a test case class, or a callable object which
142n/a returns a TestCase or TestSuite instance.
143n/a
144n/a The method optionally resolves the names relative to a given module.
145n/a """
146n/a parts = name.split('.')
147n/a error_case, error_message = None, None
148n/a if module is None:
149n/a parts_copy = parts[:]
150n/a while parts_copy:
151n/a try:
152n/a module_name = '.'.join(parts_copy)
153n/a module = __import__(module_name)
154n/a break
155n/a except ImportError:
156n/a next_attribute = parts_copy.pop()
157n/a # Last error so we can give it to the user if needed.
158n/a error_case, error_message = _make_failed_import_test(
159n/a next_attribute, self.suiteClass)
160n/a if not parts_copy:
161n/a # Even the top level import failed: report that error.
162n/a self.errors.append(error_message)
163n/a return error_case
164n/a parts = parts[1:]
165n/a obj = module
166n/a for part in parts:
167n/a try:
168n/a parent, obj = obj, getattr(obj, part)
169n/a except AttributeError as e:
170n/a # We can't traverse some part of the name.
171n/a if (getattr(obj, '__path__', None) is not None
172n/a and error_case is not None):
173n/a # This is a package (no __path__ per importlib docs), and we
174n/a # encountered an error importing something. We cannot tell
175n/a # the difference between package.WrongNameTestClass and
176n/a # package.wrong_module_name so we just report the
177n/a # ImportError - it is more informative.
178n/a self.errors.append(error_message)
179n/a return error_case
180n/a else:
181n/a # Otherwise, we signal that an AttributeError has occurred.
182n/a error_case, error_message = _make_failed_test(
183n/a part, e, self.suiteClass,
184n/a 'Failed to access attribute:\n%s' % (
185n/a traceback.format_exc(),))
186n/a self.errors.append(error_message)
187n/a return error_case
188n/a
189n/a if isinstance(obj, types.ModuleType):
190n/a return self.loadTestsFromModule(obj)
191n/a elif isinstance(obj, type) and issubclass(obj, case.TestCase):
192n/a return self.loadTestsFromTestCase(obj)
193n/a elif (isinstance(obj, types.FunctionType) and
194n/a isinstance(parent, type) and
195n/a issubclass(parent, case.TestCase)):
196n/a name = parts[-1]
197n/a inst = parent(name)
198n/a # static methods follow a different path
199n/a if not isinstance(getattr(inst, name), types.FunctionType):
200n/a return self.suiteClass([inst])
201n/a elif isinstance(obj, suite.TestSuite):
202n/a return obj
203n/a if callable(obj):
204n/a test = obj()
205n/a if isinstance(test, suite.TestSuite):
206n/a return test
207n/a elif isinstance(test, case.TestCase):
208n/a return self.suiteClass([test])
209n/a else:
210n/a raise TypeError("calling %s returned %s, not a test" %
211n/a (obj, test))
212n/a else:
213n/a raise TypeError("don't know how to make test from: %s" % obj)
214n/a
215n/a def loadTestsFromNames(self, names, module=None):
216n/a """Return a suite of all test cases found using the given sequence
217n/a of string specifiers. See 'loadTestsFromName()'.
218n/a """
219n/a suites = [self.loadTestsFromName(name, module) for name in names]
220n/a return self.suiteClass(suites)
221n/a
222n/a def getTestCaseNames(self, testCaseClass):
223n/a """Return a sorted sequence of method names found within testCaseClass
224n/a """
225n/a def isTestMethod(attrname, testCaseClass=testCaseClass,
226n/a prefix=self.testMethodPrefix):
227n/a return attrname.startswith(prefix) and \
228n/a callable(getattr(testCaseClass, attrname))
229n/a testFnNames = list(filter(isTestMethod, dir(testCaseClass)))
230n/a if self.sortTestMethodsUsing:
231n/a testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
232n/a return testFnNames
233n/a
234n/a def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
235n/a """Find and return all test modules from the specified start
236n/a directory, recursing into subdirectories to find them and return all
237n/a tests found within them. Only test files that match the pattern will
238n/a be loaded. (Using shell style pattern matching.)
239n/a
240n/a All test modules must be importable from the top level of the project.
241n/a If the start directory is not the top level directory then the top
242n/a level directory must be specified separately.
243n/a
244n/a If a test package name (directory with '__init__.py') matches the
245n/a pattern then the package will be checked for a 'load_tests' function. If
246n/a this exists then it will be called with (loader, tests, pattern) unless
247n/a the package has already had load_tests called from the same discovery
248n/a invocation, in which case the package module object is not scanned for
249n/a tests - this ensures that when a package uses discover to further
250n/a discover child tests that infinite recursion does not happen.
251n/a
252n/a If load_tests exists then discovery does *not* recurse into the package,
253n/a load_tests is responsible for loading all tests in the package.
254n/a
255n/a The pattern is deliberately not stored as a loader attribute so that
256n/a packages can continue discovery themselves. top_level_dir is stored so
257n/a load_tests does not need to pass this argument in to loader.discover().
258n/a
259n/a Paths are sorted before being imported to ensure reproducible execution
260n/a order even on filesystems with non-alphabetical ordering like ext3/4.
261n/a """
262n/a set_implicit_top = False
263n/a if top_level_dir is None and self._top_level_dir is not None:
264n/a # make top_level_dir optional if called from load_tests in a package
265n/a top_level_dir = self._top_level_dir
266n/a elif top_level_dir is None:
267n/a set_implicit_top = True
268n/a top_level_dir = start_dir
269n/a
270n/a top_level_dir = os.path.abspath(top_level_dir)
271n/a
272n/a if not top_level_dir in sys.path:
273n/a # all test modules must be importable from the top level directory
274n/a # should we *unconditionally* put the start directory in first
275n/a # in sys.path to minimise likelihood of conflicts between installed
276n/a # modules and development versions?
277n/a sys.path.insert(0, top_level_dir)
278n/a self._top_level_dir = top_level_dir
279n/a
280n/a is_not_importable = False
281n/a is_namespace = False
282n/a tests = []
283n/a if os.path.isdir(os.path.abspath(start_dir)):
284n/a start_dir = os.path.abspath(start_dir)
285n/a if start_dir != top_level_dir:
286n/a is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
287n/a else:
288n/a # support for discovery from dotted module names
289n/a try:
290n/a __import__(start_dir)
291n/a except ImportError:
292n/a is_not_importable = True
293n/a else:
294n/a the_module = sys.modules[start_dir]
295n/a top_part = start_dir.split('.')[0]
296n/a try:
297n/a start_dir = os.path.abspath(
298n/a os.path.dirname((the_module.__file__)))
299n/a except AttributeError:
300n/a # look for namespace packages
301n/a try:
302n/a spec = the_module.__spec__
303n/a except AttributeError:
304n/a spec = None
305n/a
306n/a if spec and spec.loader is None:
307n/a if spec.submodule_search_locations is not None:
308n/a is_namespace = True
309n/a
310n/a for path in the_module.__path__:
311n/a if (not set_implicit_top and
312n/a not path.startswith(top_level_dir)):
313n/a continue
314n/a self._top_level_dir = \
315n/a (path.split(the_module.__name__
316n/a .replace(".", os.path.sep))[0])
317n/a tests.extend(self._find_tests(path,
318n/a pattern,
319n/a namespace=True))
320n/a elif the_module.__name__ in sys.builtin_module_names:
321n/a # builtin module
322n/a raise TypeError('Can not use builtin modules '
323n/a 'as dotted module names') from None
324n/a else:
325n/a raise TypeError(
326n/a 'don\'t know how to discover from {!r}'
327n/a .format(the_module)) from None
328n/a
329n/a if set_implicit_top:
330n/a if not is_namespace:
331n/a self._top_level_dir = \
332n/a self._get_directory_containing_module(top_part)
333n/a sys.path.remove(top_level_dir)
334n/a else:
335n/a sys.path.remove(top_level_dir)
336n/a
337n/a if is_not_importable:
338n/a raise ImportError('Start directory is not importable: %r' % start_dir)
339n/a
340n/a if not is_namespace:
341n/a tests = list(self._find_tests(start_dir, pattern))
342n/a return self.suiteClass(tests)
343n/a
344n/a def _get_directory_containing_module(self, module_name):
345n/a module = sys.modules[module_name]
346n/a full_path = os.path.abspath(module.__file__)
347n/a
348n/a if os.path.basename(full_path).lower().startswith('__init__.py'):
349n/a return os.path.dirname(os.path.dirname(full_path))
350n/a else:
351n/a # here we have been given a module rather than a package - so
352n/a # all we can do is search the *same* directory the module is in
353n/a # should an exception be raised instead
354n/a return os.path.dirname(full_path)
355n/a
356n/a def _get_name_from_path(self, path):
357n/a if path == self._top_level_dir:
358n/a return '.'
359n/a path = _jython_aware_splitext(os.path.normpath(path))
360n/a
361n/a _relpath = os.path.relpath(path, self._top_level_dir)
362n/a assert not os.path.isabs(_relpath), "Path must be within the project"
363n/a assert not _relpath.startswith('..'), "Path must be within the project"
364n/a
365n/a name = _relpath.replace(os.path.sep, '.')
366n/a return name
367n/a
368n/a def _get_module_from_name(self, name):
369n/a __import__(name)
370n/a return sys.modules[name]
371n/a
372n/a def _match_path(self, path, full_path, pattern):
373n/a # override this method to use alternative matching strategy
374n/a return fnmatch(path, pattern)
375n/a
376n/a def _find_tests(self, start_dir, pattern, namespace=False):
377n/a """Used by discovery. Yields test suites it loads."""
378n/a # Handle the __init__ in this package
379n/a name = self._get_name_from_path(start_dir)
380n/a # name is '.' when start_dir == top_level_dir (and top_level_dir is by
381n/a # definition not a package).
382n/a if name != '.' and name not in self._loading_packages:
383n/a # name is in self._loading_packages while we have called into
384n/a # loadTestsFromModule with name.
385n/a tests, should_recurse = self._find_test_path(
386n/a start_dir, pattern, namespace)
387n/a if tests is not None:
388n/a yield tests
389n/a if not should_recurse:
390n/a # Either an error occurred, or load_tests was used by the
391n/a # package.
392n/a return
393n/a # Handle the contents.
394n/a paths = sorted(os.listdir(start_dir))
395n/a for path in paths:
396n/a full_path = os.path.join(start_dir, path)
397n/a tests, should_recurse = self._find_test_path(
398n/a full_path, pattern, namespace)
399n/a if tests is not None:
400n/a yield tests
401n/a if should_recurse:
402n/a # we found a package that didn't use load_tests.
403n/a name = self._get_name_from_path(full_path)
404n/a self._loading_packages.add(name)
405n/a try:
406n/a yield from self._find_tests(full_path, pattern, namespace)
407n/a finally:
408n/a self._loading_packages.discard(name)
409n/a
410n/a def _find_test_path(self, full_path, pattern, namespace=False):
411n/a """Used by discovery.
412n/a
413n/a Loads tests from a single file, or a directories' __init__.py when
414n/a passed the directory.
415n/a
416n/a Returns a tuple (None_or_tests_from_file, should_recurse).
417n/a """
418n/a basename = os.path.basename(full_path)
419n/a if os.path.isfile(full_path):
420n/a if not VALID_MODULE_NAME.match(basename):
421n/a # valid Python identifiers only
422n/a return None, False
423n/a if not self._match_path(basename, full_path, pattern):
424n/a return None, False
425n/a # if the test file matches, load it
426n/a name = self._get_name_from_path(full_path)
427n/a try:
428n/a module = self._get_module_from_name(name)
429n/a except case.SkipTest as e:
430n/a return _make_skipped_test(name, e, self.suiteClass), False
431n/a except:
432n/a error_case, error_message = \
433n/a _make_failed_import_test(name, self.suiteClass)
434n/a self.errors.append(error_message)
435n/a return error_case, False
436n/a else:
437n/a mod_file = os.path.abspath(
438n/a getattr(module, '__file__', full_path))
439n/a realpath = _jython_aware_splitext(
440n/a os.path.realpath(mod_file))
441n/a fullpath_noext = _jython_aware_splitext(
442n/a os.path.realpath(full_path))
443n/a if realpath.lower() != fullpath_noext.lower():
444n/a module_dir = os.path.dirname(realpath)
445n/a mod_name = _jython_aware_splitext(
446n/a os.path.basename(full_path))
447n/a expected_dir = os.path.dirname(full_path)
448n/a msg = ("%r module incorrectly imported from %r. Expected "
449n/a "%r. Is this module globally installed?")
450n/a raise ImportError(
451n/a msg % (mod_name, module_dir, expected_dir))
452n/a return self.loadTestsFromModule(module, pattern=pattern), False
453n/a elif os.path.isdir(full_path):
454n/a if (not namespace and
455n/a not os.path.isfile(os.path.join(full_path, '__init__.py'))):
456n/a return None, False
457n/a
458n/a load_tests = None
459n/a tests = None
460n/a name = self._get_name_from_path(full_path)
461n/a try:
462n/a package = self._get_module_from_name(name)
463n/a except case.SkipTest as e:
464n/a return _make_skipped_test(name, e, self.suiteClass), False
465n/a except:
466n/a error_case, error_message = \
467n/a _make_failed_import_test(name, self.suiteClass)
468n/a self.errors.append(error_message)
469n/a return error_case, False
470n/a else:
471n/a load_tests = getattr(package, 'load_tests', None)
472n/a # Mark this package as being in load_tests (possibly ;))
473n/a self._loading_packages.add(name)
474n/a try:
475n/a tests = self.loadTestsFromModule(package, pattern=pattern)
476n/a if load_tests is not None:
477n/a # loadTestsFromModule(package) has loaded tests for us.
478n/a return tests, False
479n/a return tests, True
480n/a finally:
481n/a self._loading_packages.discard(name)
482n/a else:
483n/a return None, False
484n/a
485n/a
486n/adefaultTestLoader = TestLoader()
487n/a
488n/a
489n/adef _makeLoader(prefix, sortUsing, suiteClass=None):
490n/a loader = TestLoader()
491n/a loader.sortTestMethodsUsing = sortUsing
492n/a loader.testMethodPrefix = prefix
493n/a if suiteClass:
494n/a loader.suiteClass = suiteClass
495n/a return loader
496n/a
497n/adef getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp):
498n/a return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
499n/a
500n/adef makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
501n/a suiteClass=suite.TestSuite):
502n/a return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
503n/a testCaseClass)
504n/a
505n/adef findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
506n/a suiteClass=suite.TestSuite):
507n/a return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
508n/a module)