Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2..../unittest
File: main.py
"""Unittest main program"""
[0] Fix | Delete
[1] Fix | Delete
import sys
[2] Fix | Delete
import os
[3] Fix | Delete
import types
[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
FAILFAST = " -f, --failfast Stop on first failure\n"
[11] Fix | Delete
CATCHBREAK = " -c, --catch Catch control-C and display results\n"
[12] Fix | Delete
BUFFEROUTPUT = " -b, --buffer Buffer stdout and stderr during test runs\n"
[13] Fix | Delete
[14] Fix | Delete
USAGE_AS_MAIN = """\
[15] Fix | Delete
Usage: %(progName)s [options] [tests]
[16] Fix | Delete
[17] Fix | Delete
Options:
[18] Fix | Delete
-h, --help Show this message
[19] Fix | Delete
-v, --verbose Verbose output
[20] Fix | Delete
-q, --quiet Minimal output
[21] Fix | Delete
%(failfast)s%(catchbreak)s%(buffer)s
[22] Fix | Delete
Examples:
[23] Fix | Delete
%(progName)s test_module - run tests from test_module
[24] Fix | Delete
%(progName)s module.TestClass - run tests from module.TestClass
[25] Fix | Delete
%(progName)s module.Class.test_method - run specified test method
[26] Fix | Delete
[27] Fix | Delete
[tests] can be a list of any number of test modules, classes and test
[28] Fix | Delete
methods.
[29] Fix | Delete
[30] Fix | Delete
Alternative Usage: %(progName)s discover [options]
[31] Fix | Delete
[32] Fix | Delete
Options:
[33] Fix | Delete
-v, --verbose Verbose output
[34] Fix | Delete
%(failfast)s%(catchbreak)s%(buffer)s -s directory Directory to start discovery ('.' default)
[35] Fix | Delete
-p pattern Pattern to match test files ('test*.py' default)
[36] Fix | Delete
-t directory Top level directory of project (default to
[37] Fix | Delete
start directory)
[38] Fix | Delete
[39] Fix | Delete
For test discovery all test modules must be importable from the top
[40] Fix | Delete
level directory of the project.
[41] Fix | Delete
"""
[42] Fix | Delete
[43] Fix | Delete
USAGE_FROM_MODULE = """\
[44] Fix | Delete
Usage: %(progName)s [options] [test] [...]
[45] Fix | Delete
[46] Fix | Delete
Options:
[47] Fix | Delete
-h, --help Show this message
[48] Fix | Delete
-v, --verbose Verbose output
[49] Fix | Delete
-q, --quiet Minimal output
[50] Fix | Delete
%(failfast)s%(catchbreak)s%(buffer)s
[51] Fix | Delete
Examples:
[52] Fix | Delete
%(progName)s - run default set of tests
[53] Fix | Delete
%(progName)s MyTestSuite - run suite 'MyTestSuite'
[54] Fix | Delete
%(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
[55] Fix | Delete
%(progName)s MyTestCase - run all 'test*' test methods
[56] Fix | Delete
in MyTestCase
[57] Fix | Delete
"""
[58] Fix | Delete
[59] Fix | Delete
[60] Fix | Delete
[61] Fix | Delete
class TestProgram(object):
[62] Fix | Delete
"""A command-line program that runs a set of tests; this is primarily
[63] Fix | Delete
for making test modules conveniently executable.
[64] Fix | Delete
"""
[65] Fix | Delete
USAGE = USAGE_FROM_MODULE
[66] Fix | Delete
[67] Fix | Delete
# defaults for testing
[68] Fix | Delete
failfast = catchbreak = buffer = progName = None
[69] Fix | Delete
[70] Fix | Delete
def __init__(self, module='__main__', defaultTest=None, argv=None,
[71] Fix | Delete
testRunner=None, testLoader=loader.defaultTestLoader,
[72] Fix | Delete
exit=True, verbosity=1, failfast=None, catchbreak=None,
[73] Fix | Delete
buffer=None):
[74] Fix | Delete
if isinstance(module, basestring):
[75] Fix | Delete
self.module = __import__(module)
[76] Fix | Delete
for part in module.split('.')[1:]:
[77] Fix | Delete
self.module = getattr(self.module, part)
[78] Fix | Delete
else:
[79] Fix | Delete
self.module = module
[80] Fix | Delete
if argv is None:
[81] Fix | Delete
argv = sys.argv
[82] Fix | Delete
[83] Fix | Delete
self.exit = exit
[84] Fix | Delete
self.failfast = failfast
[85] Fix | Delete
self.catchbreak = catchbreak
[86] Fix | Delete
self.verbosity = verbosity
[87] Fix | Delete
self.buffer = buffer
[88] Fix | Delete
self.defaultTest = defaultTest
[89] Fix | Delete
self.testRunner = testRunner
[90] Fix | Delete
self.testLoader = testLoader
[91] Fix | Delete
self.progName = os.path.basename(argv[0])
[92] Fix | Delete
self.parseArgs(argv)
[93] Fix | Delete
self.runTests()
[94] Fix | Delete
[95] Fix | Delete
def usageExit(self, msg=None):
[96] Fix | Delete
if msg:
[97] Fix | Delete
print msg
[98] Fix | Delete
usage = {'progName': self.progName, 'catchbreak': '', 'failfast': '',
[99] Fix | Delete
'buffer': ''}
[100] Fix | Delete
if self.failfast != False:
[101] Fix | Delete
usage['failfast'] = FAILFAST
[102] Fix | Delete
if self.catchbreak != False:
[103] Fix | Delete
usage['catchbreak'] = CATCHBREAK
[104] Fix | Delete
if self.buffer != False:
[105] Fix | Delete
usage['buffer'] = BUFFEROUTPUT
[106] Fix | Delete
print self.USAGE % usage
[107] Fix | Delete
sys.exit(2)
[108] Fix | Delete
[109] Fix | Delete
def parseArgs(self, argv):
[110] Fix | Delete
if len(argv) > 1 and argv[1].lower() == 'discover':
[111] Fix | Delete
self._do_discovery(argv[2:])
[112] Fix | Delete
return
[113] Fix | Delete
[114] Fix | Delete
import getopt
[115] Fix | Delete
long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch', 'buffer']
[116] Fix | Delete
try:
[117] Fix | Delete
options, args = getopt.getopt(argv[1:], 'hHvqfcb', long_opts)
[118] Fix | Delete
for opt, value in options:
[119] Fix | Delete
if opt in ('-h','-H','--help'):
[120] Fix | Delete
self.usageExit()
[121] Fix | Delete
if opt in ('-q','--quiet'):
[122] Fix | Delete
self.verbosity = 0
[123] Fix | Delete
if opt in ('-v','--verbose'):
[124] Fix | Delete
self.verbosity = 2
[125] Fix | Delete
if opt in ('-f','--failfast'):
[126] Fix | Delete
if self.failfast is None:
[127] Fix | Delete
self.failfast = True
[128] Fix | Delete
# Should this raise an exception if -f is not valid?
[129] Fix | Delete
if opt in ('-c','--catch'):
[130] Fix | Delete
if self.catchbreak is None:
[131] Fix | Delete
self.catchbreak = True
[132] Fix | Delete
# Should this raise an exception if -c is not valid?
[133] Fix | Delete
if opt in ('-b','--buffer'):
[134] Fix | Delete
if self.buffer is None:
[135] Fix | Delete
self.buffer = True
[136] Fix | Delete
# Should this raise an exception if -b is not valid?
[137] Fix | Delete
if len(args) == 0 and self.defaultTest is None:
[138] Fix | Delete
# createTests will load tests from self.module
[139] Fix | Delete
self.testNames = None
[140] Fix | Delete
elif len(args) > 0:
[141] Fix | Delete
self.testNames = args
[142] Fix | Delete
if __name__ == '__main__':
[143] Fix | Delete
# to support python -m unittest ...
[144] Fix | Delete
self.module = None
[145] Fix | Delete
else:
[146] Fix | Delete
self.testNames = (self.defaultTest,)
[147] Fix | Delete
self.createTests()
[148] Fix | Delete
except getopt.error, msg:
[149] Fix | Delete
self.usageExit(msg)
[150] Fix | Delete
[151] Fix | Delete
def createTests(self):
[152] Fix | Delete
if self.testNames is None:
[153] Fix | Delete
self.test = self.testLoader.loadTestsFromModule(self.module)
[154] Fix | Delete
else:
[155] Fix | Delete
self.test = self.testLoader.loadTestsFromNames(self.testNames,
[156] Fix | Delete
self.module)
[157] Fix | Delete
[158] Fix | Delete
def _do_discovery(self, argv, Loader=None):
[159] Fix | Delete
if Loader is None:
[160] Fix | Delete
Loader = lambda: self.testLoader
[161] Fix | Delete
[162] Fix | Delete
# handle command line args for test discovery
[163] Fix | Delete
self.progName = '%s discover' % self.progName
[164] Fix | Delete
import optparse
[165] Fix | Delete
parser = optparse.OptionParser()
[166] Fix | Delete
parser.prog = self.progName
[167] Fix | Delete
parser.add_option('-v', '--verbose', dest='verbose', default=False,
[168] Fix | Delete
help='Verbose output', action='store_true')
[169] Fix | Delete
if self.failfast != False:
[170] Fix | Delete
parser.add_option('-f', '--failfast', dest='failfast', default=False,
[171] Fix | Delete
help='Stop on first fail or error',
[172] Fix | Delete
action='store_true')
[173] Fix | Delete
if self.catchbreak != False:
[174] Fix | Delete
parser.add_option('-c', '--catch', dest='catchbreak', default=False,
[175] Fix | Delete
help='Catch Ctrl-C and display results so far',
[176] Fix | Delete
action='store_true')
[177] Fix | Delete
if self.buffer != False:
[178] Fix | Delete
parser.add_option('-b', '--buffer', dest='buffer', default=False,
[179] Fix | Delete
help='Buffer stdout and stderr during tests',
[180] Fix | Delete
action='store_true')
[181] Fix | Delete
parser.add_option('-s', '--start-directory', dest='start', default='.',
[182] Fix | Delete
help="Directory to start discovery ('.' default)")
[183] Fix | Delete
parser.add_option('-p', '--pattern', dest='pattern', default='test*.py',
[184] Fix | Delete
help="Pattern to match tests ('test*.py' default)")
[185] Fix | Delete
parser.add_option('-t', '--top-level-directory', dest='top', default=None,
[186] Fix | Delete
help='Top level directory of project (defaults to start directory)')
[187] Fix | Delete
[188] Fix | Delete
options, args = parser.parse_args(argv)
[189] Fix | Delete
if len(args) > 3:
[190] Fix | Delete
self.usageExit()
[191] Fix | Delete
[192] Fix | Delete
for name, value in zip(('start', 'pattern', 'top'), args):
[193] Fix | Delete
setattr(options, name, value)
[194] Fix | Delete
[195] Fix | Delete
# only set options from the parsing here
[196] Fix | Delete
# if they weren't set explicitly in the constructor
[197] Fix | Delete
if self.failfast is None:
[198] Fix | Delete
self.failfast = options.failfast
[199] Fix | Delete
if self.catchbreak is None:
[200] Fix | Delete
self.catchbreak = options.catchbreak
[201] Fix | Delete
if self.buffer is None:
[202] Fix | Delete
self.buffer = options.buffer
[203] Fix | Delete
[204] Fix | Delete
if options.verbose:
[205] Fix | Delete
self.verbosity = 2
[206] Fix | Delete
[207] Fix | Delete
start_dir = options.start
[208] Fix | Delete
pattern = options.pattern
[209] Fix | Delete
top_level_dir = options.top
[210] Fix | Delete
[211] Fix | Delete
loader = Loader()
[212] Fix | Delete
self.test = loader.discover(start_dir, pattern, top_level_dir)
[213] Fix | Delete
[214] Fix | Delete
def runTests(self):
[215] Fix | Delete
if self.catchbreak:
[216] Fix | Delete
installHandler()
[217] Fix | Delete
if self.testRunner is None:
[218] Fix | Delete
self.testRunner = runner.TextTestRunner
[219] Fix | Delete
if isinstance(self.testRunner, (type, types.ClassType)):
[220] Fix | Delete
try:
[221] Fix | Delete
testRunner = self.testRunner(verbosity=self.verbosity,
[222] Fix | Delete
failfast=self.failfast,
[223] Fix | Delete
buffer=self.buffer)
[224] Fix | Delete
except TypeError:
[225] Fix | Delete
# didn't accept the verbosity, buffer or failfast arguments
[226] Fix | Delete
testRunner = self.testRunner()
[227] Fix | Delete
else:
[228] Fix | Delete
# it is assumed to be a TestRunner instance
[229] Fix | Delete
testRunner = self.testRunner
[230] Fix | Delete
self.result = testRunner.run(self.test)
[231] Fix | Delete
if self.exit:
[232] Fix | Delete
sys.exit(not self.result.wasSuccessful())
[233] Fix | Delete
[234] Fix | Delete
main = TestProgram
[235] Fix | Delete
[236] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function