mirror of
https://github.com/python/cpython.git
synced 2025-07-27 05:04:15 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r79464 | michael.foord | 2010-03-27 07:55:19 -0500 (Sat, 27 Mar 2010) | 1 line A fix for running unittest tests on platforms without the audioop module (e.g. jython and IronPython) ........ r79471 | michael.foord | 2010-03-27 14:10:11 -0500 (Sat, 27 Mar 2010) | 4 lines Addition of delta keyword argument to unittest.TestCase.assertAlmostEquals and assertNotAlmostEquals This allows the comparison of objects by specifying a maximum difference; this includes the comparing of non-numeric objects that don't support rounding. ........ r79623 | michael.foord | 2010-04-02 16:42:47 -0500 (Fri, 02 Apr 2010) | 1 line Addition of -b command line option to unittest for buffering stdout and stderr during test runs. ........ r79626 | michael.foord | 2010-04-02 17:08:29 -0500 (Fri, 02 Apr 2010) | 1 line TestResult stores original sys.stdout and tests no longer use sys.__stdout__ (etc) in tests for unittest -b command line option ........ r79630 | michael.foord | 2010-04-02 17:30:56 -0500 (Fri, 02 Apr 2010) | 1 line unittest tests no longer replace the sys.stdout put in place by regrtest ........ r79632 | michael.foord | 2010-04-02 17:55:59 -0500 (Fri, 02 Apr 2010) | 1 line Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches ........ r79643 | michael.foord | 2010-04-02 20:15:21 -0500 (Fri, 02 Apr 2010) | 1 line Support dotted module names for test discovery paths in unittest. Issue 8038. ........ r79648 | michael.foord | 2010-04-02 21:21:39 -0500 (Fri, 02 Apr 2010) | 1 line Cross platform unittest.TestResult newline handling when buffering stdout / stderr. ........ r79649 | michael.foord | 2010-04-02 21:33:55 -0500 (Fri, 02 Apr 2010) | 1 line Another attempt at a fix for unittest.test.test_result for windows line endings ........ r79679 | michael.foord | 2010-04-03 09:52:18 -0500 (Sat, 03 Apr 2010) | 1 line Adding -b command line option to the unittest usage message. ........ r79685 | michael.foord | 2010-04-03 10:20:00 -0500 (Sat, 03 Apr 2010) | 1 line Minor tweak to unittest command line usage message ........ r79711 | michael.foord | 2010-04-03 12:03:11 -0500 (Sat, 03 Apr 2010) | 1 line Documenting new features in unittest ........ r79761 | michael.foord | 2010-04-04 17:41:54 -0500 (Sun, 04 Apr 2010) | 1 line unittest documentation formatting changes ........ r79774 | michael.foord | 2010-04-04 18:28:44 -0500 (Sun, 04 Apr 2010) | 1 line Adding documentation for new unittest.main() parameters ........ r79777 | michael.foord | 2010-04-04 19:39:50 -0500 (Sun, 04 Apr 2010) | 1 line Document signal handling functions in unittest.rst ........ r79792 | michael.foord | 2010-04-05 05:26:26 -0500 (Mon, 05 Apr 2010) | 1 line Documentation fixes for unittest ........ r79793 | michael.foord | 2010-04-05 05:28:27 -0500 (Mon, 05 Apr 2010) | 1 line Furterh documentation fix for unittest.rst ........ r79794 | michael.foord | 2010-04-05 05:30:14 -0500 (Mon, 05 Apr 2010) | 1 line Further documentation fix for unittest.rst ........ r79877 | michael.foord | 2010-04-06 18:18:16 -0500 (Tue, 06 Apr 2010) | 1 line Fix module directory finding logic for dotted paths in unittest test discovery. ........ r79898 | michael.foord | 2010-04-07 18:04:22 -0500 (Wed, 07 Apr 2010) | 1 line unittest.result.TestResult does not create its buffers until they're used. It uses StringIO not cStringIO. Issue 8333. ........ r79899 | michael.foord | 2010-04-07 19:04:24 -0500 (Wed, 07 Apr 2010) | 1 line Switch regrtest to use StringIO instead of cStringIO for test_multiprocessing on Windows. Issue 8333. ........ r79900 | michael.foord | 2010-04-07 23:33:20 -0500 (Wed, 07 Apr 2010) | 1 line Correction of unittest documentation typos and omissions ........
304 lines
12 KiB
Python
304 lines
12 KiB
Python
"""Loading unittests."""
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
import traceback
|
|
import types
|
|
import functools
|
|
|
|
from fnmatch import fnmatch
|
|
|
|
from . import case, suite, util
|
|
|
|
__unittest = True
|
|
|
|
# what about .pyc or .pyo (etc)
|
|
# we would need to avoid loading the same tests multiple times
|
|
# from '.py', '.pyc' *and* '.pyo'
|
|
VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
|
|
|
|
|
|
def _make_failed_import_test(name, suiteClass):
|
|
message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc())
|
|
return _make_failed_test('ModuleImportFailure', name, ImportError(message),
|
|
suiteClass)
|
|
|
|
def _make_failed_load_tests(name, exception, suiteClass):
|
|
return _make_failed_test('LoadTestsFailure', name, exception, suiteClass)
|
|
|
|
def _make_failed_test(classname, methodname, exception, suiteClass):
|
|
def testFailure(self):
|
|
raise exception
|
|
attrs = {methodname: testFailure}
|
|
TestClass = type(classname, (case.TestCase,), attrs)
|
|
return suiteClass((TestClass(methodname),))
|
|
|
|
|
|
class TestLoader(object):
|
|
"""
|
|
This class is responsible for loading tests according to various criteria
|
|
and returning them wrapped in a TestSuite
|
|
"""
|
|
testMethodPrefix = 'test'
|
|
sortTestMethodsUsing = staticmethod(util.three_way_cmp)
|
|
suiteClass = suite.TestSuite
|
|
_top_level_dir = None
|
|
|
|
def loadTestsFromTestCase(self, testCaseClass):
|
|
"""Return a suite of all tests cases contained in testCaseClass"""
|
|
if issubclass(testCaseClass, suite.TestSuite):
|
|
raise TypeError("Test cases should not be derived from TestSuite." \
|
|
" Maybe you meant to derive from TestCase?")
|
|
testCaseNames = self.getTestCaseNames(testCaseClass)
|
|
if not testCaseNames and hasattr(testCaseClass, 'runTest'):
|
|
testCaseNames = ['runTest']
|
|
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
|
|
return loaded_suite
|
|
|
|
def loadTestsFromModule(self, module, use_load_tests=True):
|
|
"""Return a suite of all tests cases contained in the given module"""
|
|
tests = []
|
|
for name in dir(module):
|
|
obj = getattr(module, name)
|
|
if isinstance(obj, type) and issubclass(obj, case.TestCase):
|
|
tests.append(self.loadTestsFromTestCase(obj))
|
|
|
|
load_tests = getattr(module, 'load_tests', None)
|
|
tests = self.suiteClass(tests)
|
|
if use_load_tests and load_tests is not None:
|
|
try:
|
|
return load_tests(self, tests, None)
|
|
except Exception as e:
|
|
return _make_failed_load_tests(module.__name__, e,
|
|
self.suiteClass)
|
|
return tests
|
|
|
|
def loadTestsFromName(self, name, module=None):
|
|
"""Return a suite of all tests cases given a string specifier.
|
|
|
|
The name may resolve either to a module, a test case class, a
|
|
test method within a test case class, or a callable object which
|
|
returns a TestCase or TestSuite instance.
|
|
|
|
The method optionally resolves the names relative to a given module.
|
|
"""
|
|
parts = name.split('.')
|
|
if module is None:
|
|
parts_copy = parts[:]
|
|
while parts_copy:
|
|
try:
|
|
module = __import__('.'.join(parts_copy))
|
|
break
|
|
except ImportError:
|
|
del parts_copy[-1]
|
|
if not parts_copy:
|
|
raise
|
|
parts = parts[1:]
|
|
obj = module
|
|
for part in parts:
|
|
parent, obj = obj, getattr(obj, part)
|
|
|
|
if isinstance(obj, types.ModuleType):
|
|
return self.loadTestsFromModule(obj)
|
|
elif isinstance(obj, type) and issubclass(obj, case.TestCase):
|
|
return self.loadTestsFromTestCase(obj)
|
|
elif (isinstance(obj, types.FunctionType) and
|
|
isinstance(parent, type) and
|
|
issubclass(parent, case.TestCase)):
|
|
name = obj.__name__
|
|
inst = parent(name)
|
|
# static methods follow a different path
|
|
if not isinstance(getattr(inst, name), types.FunctionType):
|
|
return self.suiteClass([inst])
|
|
elif isinstance(obj, suite.TestSuite):
|
|
return obj
|
|
if hasattr(obj, '__call__'):
|
|
test = obj()
|
|
if isinstance(test, suite.TestSuite):
|
|
return test
|
|
elif isinstance(test, case.TestCase):
|
|
return self.suiteClass([test])
|
|
else:
|
|
raise TypeError("calling %s returned %s, not a test" %
|
|
(obj, test))
|
|
else:
|
|
raise TypeError("don't know how to make test from: %s" % obj)
|
|
|
|
def loadTestsFromNames(self, names, module=None):
|
|
"""Return a suite of all tests cases found using the given sequence
|
|
of string specifiers. See 'loadTestsFromName()'.
|
|
"""
|
|
suites = [self.loadTestsFromName(name, module) for name in names]
|
|
return self.suiteClass(suites)
|
|
|
|
def getTestCaseNames(self, testCaseClass):
|
|
"""Return a sorted sequence of method names found within testCaseClass
|
|
"""
|
|
def isTestMethod(attrname, testCaseClass=testCaseClass,
|
|
prefix=self.testMethodPrefix):
|
|
return attrname.startswith(prefix) and \
|
|
hasattr(getattr(testCaseClass, attrname), '__call__')
|
|
testFnNames = testFnNames = list(filter(isTestMethod,
|
|
dir(testCaseClass)))
|
|
if self.sortTestMethodsUsing:
|
|
testFnNames.sort(key=functools.cmp_to_key(self.sortTestMethodsUsing))
|
|
return testFnNames
|
|
|
|
def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
|
|
"""Find and return all test modules from the specified start
|
|
directory, recursing into subdirectories to find them. Only test files
|
|
that match the pattern will be loaded. (Using shell style pattern
|
|
matching.)
|
|
|
|
All test modules must be importable from the top level of the project.
|
|
If the start directory is not the top level directory then the top
|
|
level directory must be specified separately.
|
|
|
|
If a test package name (directory with '__init__.py') matches the
|
|
pattern then the package will be checked for a 'load_tests' function. If
|
|
this exists then it will be called with loader, tests, pattern.
|
|
|
|
If load_tests exists then discovery does *not* recurse into the package,
|
|
load_tests is responsible for loading all tests in the package.
|
|
|
|
The pattern is deliberately not stored as a loader attribute so that
|
|
packages can continue discovery themselves. top_level_dir is stored so
|
|
load_tests does not need to pass this argument in to loader.discover().
|
|
"""
|
|
set_implicit_top = False
|
|
if top_level_dir is None and self._top_level_dir is not None:
|
|
# make top_level_dir optional if called from load_tests in a package
|
|
top_level_dir = self._top_level_dir
|
|
elif top_level_dir is None:
|
|
set_implicit_top = True
|
|
top_level_dir = start_dir
|
|
|
|
top_level_dir = os.path.abspath(top_level_dir)
|
|
|
|
if not top_level_dir in sys.path:
|
|
# all test modules must be importable from the top level directory
|
|
sys.path.append(top_level_dir)
|
|
self._top_level_dir = top_level_dir
|
|
|
|
is_not_importable = False
|
|
if os.path.isdir(os.path.abspath(start_dir)):
|
|
start_dir = os.path.abspath(start_dir)
|
|
if start_dir != top_level_dir:
|
|
is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
|
|
else:
|
|
# support for discovery from dotted module names
|
|
try:
|
|
__import__(start_dir)
|
|
except ImportError:
|
|
is_not_importable = True
|
|
else:
|
|
the_module = sys.modules[start_dir]
|
|
top_part = start_dir.split('.')[0]
|
|
start_dir = os.path.abspath(os.path.dirname((the_module.__file__)))
|
|
if set_implicit_top:
|
|
self._top_level_dir = self._get_directory_containing_module(top_part)
|
|
sys.path.remove(top_level_dir)
|
|
|
|
if is_not_importable:
|
|
raise ImportError('Start directory is not importable: %r' % start_dir)
|
|
|
|
tests = list(self._find_tests(start_dir, pattern))
|
|
return self.suiteClass(tests)
|
|
|
|
def _get_directory_containing_module(self, module_name):
|
|
module = sys.modules[module_name]
|
|
full_path = os.path.abspath(module.__file__)
|
|
|
|
if os.path.basename(full_path).lower().startswith('__init__.py'):
|
|
return os.path.dirname(os.path.dirname(full_path))
|
|
else:
|
|
# here we have been given a module rather than a package - so
|
|
# all we can do is search the *same* directory the module is in
|
|
# should an exception be raised instead
|
|
return os.path.dirname(full_path)
|
|
|
|
def _get_name_from_path(self, path):
|
|
path = os.path.splitext(os.path.normpath(path))[0]
|
|
|
|
_relpath = os.path.relpath(path, self._top_level_dir)
|
|
assert not os.path.isabs(_relpath), "Path must be within the project"
|
|
assert not _relpath.startswith('..'), "Path must be within the project"
|
|
|
|
name = _relpath.replace(os.path.sep, '.')
|
|
return name
|
|
|
|
def _get_module_from_name(self, name):
|
|
__import__(name)
|
|
return sys.modules[name]
|
|
|
|
def _find_tests(self, start_dir, pattern):
|
|
"""Used by discovery. Yields test suites it loads."""
|
|
paths = os.listdir(start_dir)
|
|
|
|
for path in paths:
|
|
full_path = os.path.join(start_dir, path)
|
|
if os.path.isfile(full_path):
|
|
if not VALID_MODULE_NAME.match(path):
|
|
# valid Python identifiers only
|
|
continue
|
|
|
|
if fnmatch(path, pattern):
|
|
# if the test file matches, load it
|
|
name = self._get_name_from_path(full_path)
|
|
try:
|
|
module = self._get_module_from_name(name)
|
|
except:
|
|
yield _make_failed_import_test(name, self.suiteClass)
|
|
else:
|
|
yield self.loadTestsFromModule(module)
|
|
elif os.path.isdir(full_path):
|
|
if not os.path.isfile(os.path.join(full_path, '__init__.py')):
|
|
continue
|
|
|
|
load_tests = None
|
|
tests = None
|
|
if fnmatch(path, pattern):
|
|
# only check load_tests if the package directory itself matches the filter
|
|
name = self._get_name_from_path(full_path)
|
|
package = self._get_module_from_name(name)
|
|
load_tests = getattr(package, 'load_tests', None)
|
|
tests = self.loadTestsFromModule(package, use_load_tests=False)
|
|
|
|
if load_tests is None:
|
|
if tests is not None:
|
|
# tests loaded from package file
|
|
yield tests
|
|
# recurse into the package
|
|
for test in self._find_tests(full_path, pattern):
|
|
yield test
|
|
else:
|
|
try:
|
|
yield load_tests(self, tests, pattern)
|
|
except Exception as e:
|
|
yield _make_failed_load_tests(package.__name__, e,
|
|
self.suiteClass)
|
|
|
|
defaultTestLoader = TestLoader()
|
|
|
|
|
|
def _makeLoader(prefix, sortUsing, suiteClass=None):
|
|
loader = TestLoader()
|
|
loader.sortTestMethodsUsing = sortUsing
|
|
loader.testMethodPrefix = prefix
|
|
if suiteClass:
|
|
loader.suiteClass = suiteClass
|
|
return loader
|
|
|
|
def getTestCaseNames(testCaseClass, prefix, sortUsing=util.three_way_cmp):
|
|
return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
|
|
|
|
def makeSuite(testCaseClass, prefix='test', sortUsing=util.three_way_cmp,
|
|
suiteClass=suite.TestSuite):
|
|
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(
|
|
testCaseClass)
|
|
|
|
def findTestCases(module, prefix='test', sortUsing=util.three_way_cmp,
|
|
suiteClass=suite.TestSuite):
|
|
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(\
|
|
module)
|