Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../unittest
File: main.py
"""Unittest main program"""
[0] Fix | Delete
[1] Fix | Delete
import sys
[2] Fix | Delete
import argparse
[3] Fix | Delete
import os
[4] Fix | Delete
[5] Fix | Delete
from . import loader, runner
[6] Fix | Delete
from .signals import installHandler
[7] Fix | Delete
[8] Fix | Delete
__unittest = True
[9] Fix | Delete
[10] Fix | Delete
MAIN_EXAMPLES = """\
[11] Fix | Delete
Examples:
[12] Fix | Delete
%(prog)s test_module - run tests from test_module
[13] Fix | Delete
%(prog)s module.TestClass - run tests from module.TestClass
[14] Fix | Delete
%(prog)s module.Class.test_method - run specified test method
[15] Fix | Delete
%(prog)s path/to/test_file.py - run tests from test_file.py
[16] Fix | Delete
"""
[17] Fix | Delete
[18] Fix | Delete
MODULE_EXAMPLES = """\
[19] Fix | Delete
Examples:
[20] Fix | Delete
%(prog)s - run default set of tests
[21] Fix | Delete
%(prog)s MyTestSuite - run suite 'MyTestSuite'
[22] Fix | Delete
%(prog)s MyTestCase.testSomething - run MyTestCase.testSomething
[23] Fix | Delete
%(prog)s MyTestCase - run all 'test*' test methods
[24] Fix | Delete
in MyTestCase
[25] Fix | Delete
"""
[26] Fix | Delete
[27] Fix | Delete
def _convert_name(name):
[28] Fix | Delete
# on Linux / Mac OS X 'foo.PY' is not importable, but on
[29] Fix | Delete
# Windows it is. Simpler to do a case insensitive match
[30] Fix | Delete
# a better check would be to check that the name is a
[31] Fix | Delete
# valid Python module name.
[32] Fix | Delete
if os.path.isfile(name) and name.lower().endswith('.py'):
[33] Fix | Delete
if os.path.isabs(name):
[34] Fix | Delete
rel_path = os.path.relpath(name, os.getcwd())
[35] Fix | Delete
if os.path.isabs(rel_path) or rel_path.startswith(os.pardir):
[36] Fix | Delete
return name
[37] Fix | Delete
name = rel_path
[38] Fix | Delete
# on Windows both '\' and '/' are used as path
[39] Fix | Delete
# separators. Better to replace both than rely on os.path.sep
[40] Fix | Delete
return name[:-3].replace('\\', '.').replace('/', '.')
[41] Fix | Delete
return name
[42] Fix | Delete
[43] Fix | Delete
def _convert_names(names):
[44] Fix | Delete
return [_convert_name(name) for name in names]
[45] Fix | Delete
[46] Fix | Delete
[47] Fix | Delete
def _convert_select_pattern(pattern):
[48] Fix | Delete
if not '*' in pattern:
[49] Fix | Delete
pattern = '*%s*' % pattern
[50] Fix | Delete
return pattern
[51] Fix | Delete
[52] Fix | Delete
[53] Fix | Delete
class TestProgram(object):
[54] Fix | Delete
"""A command-line program that runs a set of tests; this is primarily
[55] Fix | Delete
for making test modules conveniently executable.
[56] Fix | Delete
"""
[57] Fix | Delete
# defaults for testing
[58] Fix | Delete
module=None
[59] Fix | Delete
verbosity = 1
[60] Fix | Delete
failfast = catchbreak = buffer = progName = warnings = testNamePatterns = None
[61] Fix | Delete
_discovery_parser = None
[62] Fix | Delete
[63] Fix | Delete
def __init__(self, module='__main__', defaultTest=None, argv=None,
[64] Fix | Delete
testRunner=None, testLoader=loader.defaultTestLoader,
[65] Fix | Delete
exit=True, verbosity=1, failfast=None, catchbreak=None,
[66] Fix | Delete
buffer=None, warnings=None, *, tb_locals=False):
[67] Fix | Delete
if isinstance(module, str):
[68] Fix | Delete
self.module = __import__(module)
[69] Fix | Delete
for part in module.split('.')[1:]:
[70] Fix | Delete
self.module = getattr(self.module, part)
[71] Fix | Delete
else:
[72] Fix | Delete
self.module = module
[73] Fix | Delete
if argv is None:
[74] Fix | Delete
argv = sys.argv
[75] Fix | Delete
[76] Fix | Delete
self.exit = exit
[77] Fix | Delete
self.failfast = failfast
[78] Fix | Delete
self.catchbreak = catchbreak
[79] Fix | Delete
self.verbosity = verbosity
[80] Fix | Delete
self.buffer = buffer
[81] Fix | Delete
self.tb_locals = tb_locals
[82] Fix | Delete
if warnings is None and not sys.warnoptions:
[83] Fix | Delete
# even if DeprecationWarnings are ignored by default
[84] Fix | Delete
# print them anyway unless other warnings settings are
[85] Fix | Delete
# specified by the warnings arg or the -W python flag
[86] Fix | Delete
self.warnings = 'default'
[87] Fix | Delete
else:
[88] Fix | Delete
# here self.warnings is set either to the value passed
[89] Fix | Delete
# to the warnings args or to None.
[90] Fix | Delete
# If the user didn't pass a value self.warnings will
[91] Fix | Delete
# be None. This means that the behavior is unchanged
[92] Fix | Delete
# and depends on the values passed to -W.
[93] Fix | Delete
self.warnings = warnings
[94] Fix | Delete
self.defaultTest = defaultTest
[95] Fix | Delete
self.testRunner = testRunner
[96] Fix | Delete
self.testLoader = testLoader
[97] Fix | Delete
self.progName = os.path.basename(argv[0])
[98] Fix | Delete
self.parseArgs(argv)
[99] Fix | Delete
self.runTests()
[100] Fix | Delete
[101] Fix | Delete
def usageExit(self, msg=None):
[102] Fix | Delete
if msg:
[103] Fix | Delete
print(msg)
[104] Fix | Delete
if self._discovery_parser is None:
[105] Fix | Delete
self._initArgParsers()
[106] Fix | Delete
self._print_help()
[107] Fix | Delete
sys.exit(2)
[108] Fix | Delete
[109] Fix | Delete
def _print_help(self, *args, **kwargs):
[110] Fix | Delete
if self.module is None:
[111] Fix | Delete
print(self._main_parser.format_help())
[112] Fix | Delete
print(MAIN_EXAMPLES % {'prog': self.progName})
[113] Fix | Delete
self._discovery_parser.print_help()
[114] Fix | Delete
else:
[115] Fix | Delete
print(self._main_parser.format_help())
[116] Fix | Delete
print(MODULE_EXAMPLES % {'prog': self.progName})
[117] Fix | Delete
[118] Fix | Delete
def parseArgs(self, argv):
[119] Fix | Delete
self._initArgParsers()
[120] Fix | Delete
if self.module is None:
[121] Fix | Delete
if len(argv) > 1 and argv[1].lower() == 'discover':
[122] Fix | Delete
self._do_discovery(argv[2:])
[123] Fix | Delete
return
[124] Fix | Delete
self._main_parser.parse_args(argv[1:], self)
[125] Fix | Delete
if not self.tests:
[126] Fix | Delete
# this allows "python -m unittest -v" to still work for
[127] Fix | Delete
# test discovery.
[128] Fix | Delete
self._do_discovery([])
[129] Fix | Delete
return
[130] Fix | Delete
else:
[131] Fix | Delete
self._main_parser.parse_args(argv[1:], self)
[132] Fix | Delete
[133] Fix | Delete
if self.tests:
[134] Fix | Delete
self.testNames = _convert_names(self.tests)
[135] Fix | Delete
if __name__ == '__main__':
[136] Fix | Delete
# to support python -m unittest ...
[137] Fix | Delete
self.module = None
[138] Fix | Delete
elif self.defaultTest is None:
[139] Fix | Delete
# createTests will load tests from self.module
[140] Fix | Delete
self.testNames = None
[141] Fix | Delete
elif isinstance(self.defaultTest, str):
[142] Fix | Delete
self.testNames = (self.defaultTest,)
[143] Fix | Delete
else:
[144] Fix | Delete
self.testNames = list(self.defaultTest)
[145] Fix | Delete
self.createTests()
[146] Fix | Delete
[147] Fix | Delete
def createTests(self, from_discovery=False, Loader=None):
[148] Fix | Delete
if self.testNamePatterns:
[149] Fix | Delete
self.testLoader.testNamePatterns = self.testNamePatterns
[150] Fix | Delete
if from_discovery:
[151] Fix | Delete
loader = self.testLoader if Loader is None else Loader()
[152] Fix | Delete
self.test = loader.discover(self.start, self.pattern, self.top)
[153] Fix | Delete
elif self.testNames is None:
[154] Fix | Delete
self.test = self.testLoader.loadTestsFromModule(self.module)
[155] Fix | Delete
else:
[156] Fix | Delete
self.test = self.testLoader.loadTestsFromNames(self.testNames,
[157] Fix | Delete
self.module)
[158] Fix | Delete
[159] Fix | Delete
def _initArgParsers(self):
[160] Fix | Delete
parent_parser = self._getParentArgParser()
[161] Fix | Delete
self._main_parser = self._getMainArgParser(parent_parser)
[162] Fix | Delete
self._discovery_parser = self._getDiscoveryArgParser(parent_parser)
[163] Fix | Delete
[164] Fix | Delete
def _getParentArgParser(self):
[165] Fix | Delete
parser = argparse.ArgumentParser(add_help=False)
[166] Fix | Delete
[167] Fix | Delete
parser.add_argument('-v', '--verbose', dest='verbosity',
[168] Fix | Delete
action='store_const', const=2,
[169] Fix | Delete
help='Verbose output')
[170] Fix | Delete
parser.add_argument('-q', '--quiet', dest='verbosity',
[171] Fix | Delete
action='store_const', const=0,
[172] Fix | Delete
help='Quiet output')
[173] Fix | Delete
parser.add_argument('--locals', dest='tb_locals',
[174] Fix | Delete
action='store_true',
[175] Fix | Delete
help='Show local variables in tracebacks')
[176] Fix | Delete
if self.failfast is None:
[177] Fix | Delete
parser.add_argument('-f', '--failfast', dest='failfast',
[178] Fix | Delete
action='store_true',
[179] Fix | Delete
help='Stop on first fail or error')
[180] Fix | Delete
self.failfast = False
[181] Fix | Delete
if self.catchbreak is None:
[182] Fix | Delete
parser.add_argument('-c', '--catch', dest='catchbreak',
[183] Fix | Delete
action='store_true',
[184] Fix | Delete
help='Catch Ctrl-C and display results so far')
[185] Fix | Delete
self.catchbreak = False
[186] Fix | Delete
if self.buffer is None:
[187] Fix | Delete
parser.add_argument('-b', '--buffer', dest='buffer',
[188] Fix | Delete
action='store_true',
[189] Fix | Delete
help='Buffer stdout and stderr during tests')
[190] Fix | Delete
self.buffer = False
[191] Fix | Delete
if self.testNamePatterns is None:
[192] Fix | Delete
parser.add_argument('-k', dest='testNamePatterns',
[193] Fix | Delete
action='append', type=_convert_select_pattern,
[194] Fix | Delete
help='Only run tests which match the given substring')
[195] Fix | Delete
self.testNamePatterns = []
[196] Fix | Delete
[197] Fix | Delete
return parser
[198] Fix | Delete
[199] Fix | Delete
def _getMainArgParser(self, parent):
[200] Fix | Delete
parser = argparse.ArgumentParser(parents=[parent])
[201] Fix | Delete
parser.prog = self.progName
[202] Fix | Delete
parser.print_help = self._print_help
[203] Fix | Delete
[204] Fix | Delete
parser.add_argument('tests', nargs='*',
[205] Fix | Delete
help='a list of any number of test modules, '
[206] Fix | Delete
'classes and test methods.')
[207] Fix | Delete
[208] Fix | Delete
return parser
[209] Fix | Delete
[210] Fix | Delete
def _getDiscoveryArgParser(self, parent):
[211] Fix | Delete
parser = argparse.ArgumentParser(parents=[parent])
[212] Fix | Delete
parser.prog = '%s discover' % self.progName
[213] Fix | Delete
parser.epilog = ('For test discovery all test modules must be '
[214] Fix | Delete
'importable from the top level directory of the '
[215] Fix | Delete
'project.')
[216] Fix | Delete
[217] Fix | Delete
parser.add_argument('-s', '--start-directory', dest='start',
[218] Fix | Delete
help="Directory to start discovery ('.' default)")
[219] Fix | Delete
parser.add_argument('-p', '--pattern', dest='pattern',
[220] Fix | Delete
help="Pattern to match tests ('test*.py' default)")
[221] Fix | Delete
parser.add_argument('-t', '--top-level-directory', dest='top',
[222] Fix | Delete
help='Top level directory of project (defaults to '
[223] Fix | Delete
'start directory)')
[224] Fix | Delete
for arg in ('start', 'pattern', 'top'):
[225] Fix | Delete
parser.add_argument(arg, nargs='?',
[226] Fix | Delete
default=argparse.SUPPRESS,
[227] Fix | Delete
help=argparse.SUPPRESS)
[228] Fix | Delete
[229] Fix | Delete
return parser
[230] Fix | Delete
[231] Fix | Delete
def _do_discovery(self, argv, Loader=None):
[232] Fix | Delete
self.start = '.'
[233] Fix | Delete
self.pattern = 'test*.py'
[234] Fix | Delete
self.top = None
[235] Fix | Delete
if argv is not None:
[236] Fix | Delete
# handle command line args for test discovery
[237] Fix | Delete
if self._discovery_parser is None:
[238] Fix | Delete
# for testing
[239] Fix | Delete
self._initArgParsers()
[240] Fix | Delete
self._discovery_parser.parse_args(argv, self)
[241] Fix | Delete
[242] Fix | Delete
self.createTests(from_discovery=True, Loader=Loader)
[243] Fix | Delete
[244] Fix | Delete
def runTests(self):
[245] Fix | Delete
if self.catchbreak:
[246] Fix | Delete
installHandler()
[247] Fix | Delete
if self.testRunner is None:
[248] Fix | Delete
self.testRunner = runner.TextTestRunner
[249] Fix | Delete
if isinstance(self.testRunner, type):
[250] Fix | Delete
try:
[251] Fix | Delete
try:
[252] Fix | Delete
testRunner = self.testRunner(verbosity=self.verbosity,
[253] Fix | Delete
failfast=self.failfast,
[254] Fix | Delete
buffer=self.buffer,
[255] Fix | Delete
warnings=self.warnings,
[256] Fix | Delete
tb_locals=self.tb_locals)
[257] Fix | Delete
except TypeError:
[258] Fix | Delete
# didn't accept the tb_locals argument
[259] Fix | Delete
testRunner = self.testRunner(verbosity=self.verbosity,
[260] Fix | Delete
failfast=self.failfast,
[261] Fix | Delete
buffer=self.buffer,
[262] Fix | Delete
warnings=self.warnings)
[263] Fix | Delete
except TypeError:
[264] Fix | Delete
# didn't accept the verbosity, buffer or failfast arguments
[265] Fix | Delete
testRunner = self.testRunner()
[266] Fix | Delete
else:
[267] Fix | Delete
# it is assumed to be a TestRunner instance
[268] Fix | Delete
testRunner = self.testRunner
[269] Fix | Delete
self.result = testRunner.run(self.test)
[270] Fix | Delete
if self.exit:
[271] Fix | Delete
sys.exit(not self.result.wasSuccessful())
[272] Fix | Delete
[273] Fix | Delete
main = TestProgram
[274] Fix | Delete
[275] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function