Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../unittest
File: loader.py
"""Loading unittests."""
[0] Fix | Delete
[1] Fix | Delete
import os
[2] Fix | Delete
import re
[3] Fix | Delete
import sys
[4] Fix | Delete
import traceback
[5] Fix | Delete
import types
[6] Fix | Delete
[7] Fix | Delete
from functools import cmp_to_key as _CmpToKey
[8] Fix | Delete
from fnmatch import fnmatch
[9] Fix | Delete
[10] Fix | Delete
from . import case, suite
[11] Fix | Delete
[12] Fix | Delete
__unittest = True
[13] Fix | Delete
[14] Fix | Delete
# what about .pyc or .pyo (etc)
[15] Fix | Delete
# we would need to avoid loading the same tests multiple times
[16] Fix | Delete
# from '.py', '.pyc' *and* '.pyo'
[17] Fix | Delete
VALID_MODULE_NAME = re.compile(r'[_a-z]\w*\.py$', re.IGNORECASE)
[18] Fix | Delete
[19] Fix | Delete
[20] Fix | Delete
def _make_failed_import_test(name, suiteClass):
[21] Fix | Delete
message = 'Failed to import test module: %s\n%s' % (name, traceback.format_exc())
[22] Fix | Delete
return _make_failed_test('ModuleImportFailure', name, ImportError(message),
[23] Fix | Delete
suiteClass)
[24] Fix | Delete
[25] Fix | Delete
def _make_failed_load_tests(name, exception, suiteClass):
[26] Fix | Delete
return _make_failed_test('LoadTestsFailure', name, exception, suiteClass)
[27] Fix | Delete
[28] Fix | Delete
def _make_failed_test(classname, methodname, exception, suiteClass):
[29] Fix | Delete
def testFailure(self):
[30] Fix | Delete
raise exception
[31] Fix | Delete
attrs = {methodname: testFailure}
[32] Fix | Delete
TestClass = type(classname, (case.TestCase,), attrs)
[33] Fix | Delete
return suiteClass((TestClass(methodname),))
[34] Fix | Delete
[35] Fix | Delete
[36] Fix | Delete
class TestLoader(object):
[37] Fix | Delete
"""
[38] Fix | Delete
This class is responsible for loading tests according to various criteria
[39] Fix | Delete
and returning them wrapped in a TestSuite
[40] Fix | Delete
"""
[41] Fix | Delete
testMethodPrefix = 'test'
[42] Fix | Delete
sortTestMethodsUsing = cmp
[43] Fix | Delete
suiteClass = suite.TestSuite
[44] Fix | Delete
_top_level_dir = None
[45] Fix | Delete
[46] Fix | Delete
def loadTestsFromTestCase(self, testCaseClass):
[47] Fix | Delete
"""Return a suite of all test cases contained in testCaseClass"""
[48] Fix | Delete
if issubclass(testCaseClass, suite.TestSuite):
[49] Fix | Delete
raise TypeError("Test cases should not be derived from TestSuite." \
[50] Fix | Delete
" Maybe you meant to derive from TestCase?")
[51] Fix | Delete
testCaseNames = self.getTestCaseNames(testCaseClass)
[52] Fix | Delete
if not testCaseNames and hasattr(testCaseClass, 'runTest'):
[53] Fix | Delete
testCaseNames = ['runTest']
[54] Fix | Delete
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
[55] Fix | Delete
return loaded_suite
[56] Fix | Delete
[57] Fix | Delete
def loadTestsFromModule(self, module, use_load_tests=True):
[58] Fix | Delete
"""Return a suite of all test cases contained in the given module"""
[59] Fix | Delete
tests = []
[60] Fix | Delete
for name in dir(module):
[61] Fix | Delete
obj = getattr(module, name)
[62] Fix | Delete
if isinstance(obj, type) and issubclass(obj, case.TestCase):
[63] Fix | Delete
tests.append(self.loadTestsFromTestCase(obj))
[64] Fix | Delete
[65] Fix | Delete
load_tests = getattr(module, 'load_tests', None)
[66] Fix | Delete
tests = self.suiteClass(tests)
[67] Fix | Delete
if use_load_tests and load_tests is not None:
[68] Fix | Delete
try:
[69] Fix | Delete
return load_tests(self, tests, None)
[70] Fix | Delete
except Exception, e:
[71] Fix | Delete
return _make_failed_load_tests(module.__name__, e,
[72] Fix | Delete
self.suiteClass)
[73] Fix | Delete
return tests
[74] Fix | Delete
[75] Fix | Delete
def loadTestsFromName(self, name, module=None):
[76] Fix | Delete
"""Return a suite of all test cases given a string specifier.
[77] Fix | Delete
[78] Fix | Delete
The name may resolve either to a module, a test case class, a
[79] Fix | Delete
test method within a test case class, or a callable object which
[80] Fix | Delete
returns a TestCase or TestSuite instance.
[81] Fix | Delete
[82] Fix | Delete
The method optionally resolves the names relative to a given module.
[83] Fix | Delete
"""
[84] Fix | Delete
parts = name.split('.')
[85] Fix | Delete
if module is None:
[86] Fix | Delete
parts_copy = parts[:]
[87] Fix | Delete
while parts_copy:
[88] Fix | Delete
try:
[89] Fix | Delete
module = __import__('.'.join(parts_copy))
[90] Fix | Delete
break
[91] Fix | Delete
except ImportError:
[92] Fix | Delete
del parts_copy[-1]
[93] Fix | Delete
if not parts_copy:
[94] Fix | Delete
raise
[95] Fix | Delete
parts = parts[1:]
[96] Fix | Delete
obj = module
[97] Fix | Delete
for part in parts:
[98] Fix | Delete
parent, obj = obj, getattr(obj, part)
[99] Fix | Delete
[100] Fix | Delete
if isinstance(obj, types.ModuleType):
[101] Fix | Delete
return self.loadTestsFromModule(obj)
[102] Fix | Delete
elif isinstance(obj, type) and issubclass(obj, case.TestCase):
[103] Fix | Delete
return self.loadTestsFromTestCase(obj)
[104] Fix | Delete
elif (isinstance(obj, types.UnboundMethodType) and
[105] Fix | Delete
isinstance(parent, type) and
[106] Fix | Delete
issubclass(parent, case.TestCase)):
[107] Fix | Delete
name = parts[-1]
[108] Fix | Delete
inst = parent(name)
[109] Fix | Delete
return self.suiteClass([inst])
[110] Fix | Delete
elif isinstance(obj, suite.TestSuite):
[111] Fix | Delete
return obj
[112] Fix | Delete
elif hasattr(obj, '__call__'):
[113] Fix | Delete
test = obj()
[114] Fix | Delete
if isinstance(test, suite.TestSuite):
[115] Fix | Delete
return test
[116] Fix | Delete
elif isinstance(test, case.TestCase):
[117] Fix | Delete
return self.suiteClass([test])
[118] Fix | Delete
else:
[119] Fix | Delete
raise TypeError("calling %s returned %s, not a test" %
[120] Fix | Delete
(obj, test))
[121] Fix | Delete
else:
[122] Fix | Delete
raise TypeError("don't know how to make test from: %s" % obj)
[123] Fix | Delete
[124] Fix | Delete
def loadTestsFromNames(self, names, module=None):
[125] Fix | Delete
"""Return a suite of all test cases found using the given sequence
[126] Fix | Delete
of string specifiers. See 'loadTestsFromName()'.
[127] Fix | Delete
"""
[128] Fix | Delete
suites = [self.loadTestsFromName(name, module) for name in names]
[129] Fix | Delete
return self.suiteClass(suites)
[130] Fix | Delete
[131] Fix | Delete
def getTestCaseNames(self, testCaseClass):
[132] Fix | Delete
"""Return a sorted sequence of method names found within testCaseClass
[133] Fix | Delete
"""
[134] Fix | Delete
def isTestMethod(attrname, testCaseClass=testCaseClass,
[135] Fix | Delete
prefix=self.testMethodPrefix):
[136] Fix | Delete
return attrname.startswith(prefix) and \
[137] Fix | Delete
hasattr(getattr(testCaseClass, attrname), '__call__')
[138] Fix | Delete
testFnNames = filter(isTestMethod, dir(testCaseClass))
[139] Fix | Delete
if self.sortTestMethodsUsing:
[140] Fix | Delete
testFnNames.sort(key=_CmpToKey(self.sortTestMethodsUsing))
[141] Fix | Delete
return testFnNames
[142] Fix | Delete
[143] Fix | Delete
def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
[144] Fix | Delete
"""Find and return all test modules from the specified start
[145] Fix | Delete
directory, recursing into subdirectories to find them. Only test files
[146] Fix | Delete
that match the pattern will be loaded. (Using shell style pattern
[147] Fix | Delete
matching.)
[148] Fix | Delete
[149] Fix | Delete
All test modules must be importable from the top level of the project.
[150] Fix | Delete
If the start directory is not the top level directory then the top
[151] Fix | Delete
level directory must be specified separately.
[152] Fix | Delete
[153] Fix | Delete
If a test package name (directory with '__init__.py') matches the
[154] Fix | Delete
pattern then the package will be checked for a 'load_tests' function. If
[155] Fix | Delete
this exists then it will be called with loader, tests, pattern.
[156] Fix | Delete
[157] Fix | Delete
If load_tests exists then discovery does *not* recurse into the package,
[158] Fix | Delete
load_tests is responsible for loading all tests in the package.
[159] Fix | Delete
[160] Fix | Delete
The pattern is deliberately not stored as a loader attribute so that
[161] Fix | Delete
packages can continue discovery themselves. top_level_dir is stored so
[162] Fix | Delete
load_tests does not need to pass this argument in to loader.discover().
[163] Fix | Delete
"""
[164] Fix | Delete
set_implicit_top = False
[165] Fix | Delete
if top_level_dir is None and self._top_level_dir is not None:
[166] Fix | Delete
# make top_level_dir optional if called from load_tests in a package
[167] Fix | Delete
top_level_dir = self._top_level_dir
[168] Fix | Delete
elif top_level_dir is None:
[169] Fix | Delete
set_implicit_top = True
[170] Fix | Delete
top_level_dir = start_dir
[171] Fix | Delete
[172] Fix | Delete
top_level_dir = os.path.abspath(top_level_dir)
[173] Fix | Delete
[174] Fix | Delete
if not top_level_dir in sys.path:
[175] Fix | Delete
# all test modules must be importable from the top level directory
[176] Fix | Delete
# should we *unconditionally* put the start directory in first
[177] Fix | Delete
# in sys.path to minimise likelihood of conflicts between installed
[178] Fix | Delete
# modules and development versions?
[179] Fix | Delete
sys.path.insert(0, top_level_dir)
[180] Fix | Delete
self._top_level_dir = top_level_dir
[181] Fix | Delete
[182] Fix | Delete
is_not_importable = False
[183] Fix | Delete
if os.path.isdir(os.path.abspath(start_dir)):
[184] Fix | Delete
start_dir = os.path.abspath(start_dir)
[185] Fix | Delete
if start_dir != top_level_dir:
[186] Fix | Delete
is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py'))
[187] Fix | Delete
else:
[188] Fix | Delete
# support for discovery from dotted module names
[189] Fix | Delete
try:
[190] Fix | Delete
__import__(start_dir)
[191] Fix | Delete
except ImportError:
[192] Fix | Delete
is_not_importable = True
[193] Fix | Delete
else:
[194] Fix | Delete
the_module = sys.modules[start_dir]
[195] Fix | Delete
top_part = start_dir.split('.')[0]
[196] Fix | Delete
start_dir = os.path.abspath(os.path.dirname((the_module.__file__)))
[197] Fix | Delete
if set_implicit_top:
[198] Fix | Delete
self._top_level_dir = self._get_directory_containing_module(top_part)
[199] Fix | Delete
sys.path.remove(top_level_dir)
[200] Fix | Delete
[201] Fix | Delete
if is_not_importable:
[202] Fix | Delete
raise ImportError('Start directory is not importable: %r' % start_dir)
[203] Fix | Delete
[204] Fix | Delete
tests = list(self._find_tests(start_dir, pattern))
[205] Fix | Delete
return self.suiteClass(tests)
[206] Fix | Delete
[207] Fix | Delete
def _get_directory_containing_module(self, module_name):
[208] Fix | Delete
module = sys.modules[module_name]
[209] Fix | Delete
full_path = os.path.abspath(module.__file__)
[210] Fix | Delete
[211] Fix | Delete
if os.path.basename(full_path).lower().startswith('__init__.py'):
[212] Fix | Delete
return os.path.dirname(os.path.dirname(full_path))
[213] Fix | Delete
else:
[214] Fix | Delete
# here we have been given a module rather than a package - so
[215] Fix | Delete
# all we can do is search the *same* directory the module is in
[216] Fix | Delete
# should an exception be raised instead
[217] Fix | Delete
return os.path.dirname(full_path)
[218] Fix | Delete
[219] Fix | Delete
def _get_name_from_path(self, path):
[220] Fix | Delete
path = os.path.splitext(os.path.normpath(path))[0]
[221] Fix | Delete
[222] Fix | Delete
_relpath = os.path.relpath(path, self._top_level_dir)
[223] Fix | Delete
assert not os.path.isabs(_relpath), "Path must be within the project"
[224] Fix | Delete
assert not _relpath.startswith('..'), "Path must be within the project"
[225] Fix | Delete
[226] Fix | Delete
name = _relpath.replace(os.path.sep, '.')
[227] Fix | Delete
return name
[228] Fix | Delete
[229] Fix | Delete
def _get_module_from_name(self, name):
[230] Fix | Delete
__import__(name)
[231] Fix | Delete
return sys.modules[name]
[232] Fix | Delete
[233] Fix | Delete
def _match_path(self, path, full_path, pattern):
[234] Fix | Delete
# override this method to use alternative matching strategy
[235] Fix | Delete
return fnmatch(path, pattern)
[236] Fix | Delete
[237] Fix | Delete
def _find_tests(self, start_dir, pattern):
[238] Fix | Delete
"""Used by discovery. Yields test suites it loads."""
[239] Fix | Delete
paths = os.listdir(start_dir)
[240] Fix | Delete
[241] Fix | Delete
for path in paths:
[242] Fix | Delete
full_path = os.path.join(start_dir, path)
[243] Fix | Delete
if os.path.isfile(full_path):
[244] Fix | Delete
if not VALID_MODULE_NAME.match(path):
[245] Fix | Delete
# valid Python identifiers only
[246] Fix | Delete
continue
[247] Fix | Delete
if not self._match_path(path, full_path, pattern):
[248] Fix | Delete
continue
[249] Fix | Delete
# if the test file matches, load it
[250] Fix | Delete
name = self._get_name_from_path(full_path)
[251] Fix | Delete
try:
[252] Fix | Delete
module = self._get_module_from_name(name)
[253] Fix | Delete
except:
[254] Fix | Delete
yield _make_failed_import_test(name, self.suiteClass)
[255] Fix | Delete
else:
[256] Fix | Delete
mod_file = os.path.abspath(getattr(module, '__file__', full_path))
[257] Fix | Delete
realpath = os.path.splitext(os.path.realpath(mod_file))[0]
[258] Fix | Delete
fullpath_noext = os.path.splitext(os.path.realpath(full_path))[0]
[259] Fix | Delete
if realpath.lower() != fullpath_noext.lower():
[260] Fix | Delete
module_dir = os.path.dirname(realpath)
[261] Fix | Delete
mod_name = os.path.splitext(os.path.basename(full_path))[0]
[262] Fix | Delete
expected_dir = os.path.dirname(full_path)
[263] Fix | Delete
msg = ("%r module incorrectly imported from %r. Expected %r. "
[264] Fix | Delete
"Is this module globally installed?")
[265] Fix | Delete
raise ImportError(msg % (mod_name, module_dir, expected_dir))
[266] Fix | Delete
yield self.loadTestsFromModule(module)
[267] Fix | Delete
elif os.path.isdir(full_path):
[268] Fix | Delete
if not os.path.isfile(os.path.join(full_path, '__init__.py')):
[269] Fix | Delete
continue
[270] Fix | Delete
[271] Fix | Delete
load_tests = None
[272] Fix | Delete
tests = None
[273] Fix | Delete
if fnmatch(path, pattern):
[274] Fix | Delete
# only check load_tests if the package directory itself matches the filter
[275] Fix | Delete
name = self._get_name_from_path(full_path)
[276] Fix | Delete
package = self._get_module_from_name(name)
[277] Fix | Delete
load_tests = getattr(package, 'load_tests', None)
[278] Fix | Delete
tests = self.loadTestsFromModule(package, use_load_tests=False)
[279] Fix | Delete
[280] Fix | Delete
if load_tests is None:
[281] Fix | Delete
if tests is not None:
[282] Fix | Delete
# tests loaded from package file
[283] Fix | Delete
yield tests
[284] Fix | Delete
# recurse into the package
[285] Fix | Delete
for test in self._find_tests(full_path, pattern):
[286] Fix | Delete
yield test
[287] Fix | Delete
else:
[288] Fix | Delete
try:
[289] Fix | Delete
yield load_tests(self, tests, pattern)
[290] Fix | Delete
except Exception, e:
[291] Fix | Delete
yield _make_failed_load_tests(package.__name__, e,
[292] Fix | Delete
self.suiteClass)
[293] Fix | Delete
[294] Fix | Delete
defaultTestLoader = TestLoader()
[295] Fix | Delete
[296] Fix | Delete
[297] Fix | Delete
def _makeLoader(prefix, sortUsing, suiteClass=None):
[298] Fix | Delete
loader = TestLoader()
[299] Fix | Delete
loader.sortTestMethodsUsing = sortUsing
[300] Fix | Delete
loader.testMethodPrefix = prefix
[301] Fix | Delete
if suiteClass:
[302] Fix | Delete
loader.suiteClass = suiteClass
[303] Fix | Delete
return loader
[304] Fix | Delete
[305] Fix | Delete
def getTestCaseNames(testCaseClass, prefix, sortUsing=cmp):
[306] Fix | Delete
return _makeLoader(prefix, sortUsing).getTestCaseNames(testCaseClass)
[307] Fix | Delete
[308] Fix | Delete
def makeSuite(testCaseClass, prefix='test', sortUsing=cmp,
[309] Fix | Delete
suiteClass=suite.TestSuite):
[310] Fix | Delete
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
[311] Fix | Delete
[312] Fix | Delete
def findTestCases(module, prefix='test', sortUsing=cmp,
[313] Fix | Delete
suiteClass=suite.TestSuite):
[314] Fix | Delete
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromModule(module)
[315] Fix | Delete
[316] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function