2009-07-19 17:18:21 -03:00
|
|
|
"""Unittest main program"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import types
|
|
|
|
|
|
|
|
from . import loader, runner
|
2010-03-26 00:18:31 -03:00
|
|
|
from .signals import installHandler
|
2009-07-19 17:18:21 -03:00
|
|
|
|
2010-03-21 21:06:30 -03:00
|
|
|
__unittest = True
|
|
|
|
|
2009-07-19 17:18:21 -03:00
|
|
|
|
2010-03-26 00:18:31 -03:00
|
|
|
FAILFAST = " -f, --failfast Stop on first failure\n"
|
|
|
|
CATCHBREAK = " -c, --catch Catch control-C and display results\n"
|
|
|
|
|
2009-07-19 17:18:21 -03:00
|
|
|
USAGE_AS_MAIN = """\
|
|
|
|
Usage: %(progName)s [options] [tests]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help Show this message
|
|
|
|
-v, --verbose Verbose output
|
|
|
|
-q, --quiet Minimal output
|
2010-03-26 00:18:31 -03:00
|
|
|
%(failfast)s%(catchbreak)s
|
2009-07-19 17:18:21 -03:00
|
|
|
Examples:
|
|
|
|
%(progName)s test_module - run tests from test_module
|
|
|
|
%(progName)s test_module.TestClass - run tests from
|
|
|
|
test_module.TestClass
|
|
|
|
%(progName)s test_module.TestClass.test_method - run specified test method
|
|
|
|
|
|
|
|
[tests] can be a list of any number of test modules, classes and test
|
|
|
|
methods.
|
|
|
|
|
|
|
|
Alternative Usage: %(progName)s discover [options]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-v, --verbose Verbose output
|
2010-03-26 00:18:31 -03:00
|
|
|
%(failfast)s%(catchbreak)s -s directory Directory to start discovery ('.' default)
|
2009-07-19 17:18:21 -03:00
|
|
|
-p pattern Pattern to match test files ('test*.py' default)
|
|
|
|
-t directory Top level directory of project (default to
|
|
|
|
start directory)
|
|
|
|
|
|
|
|
For test discovery all test modules must be importable from the top
|
|
|
|
level directory of the project.
|
|
|
|
"""
|
|
|
|
|
|
|
|
USAGE_FROM_MODULE = """\
|
|
|
|
Usage: %(progName)s [options] [test] [...]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help Show this message
|
|
|
|
-v, --verbose Verbose output
|
|
|
|
-q, --quiet Minimal output
|
2010-03-26 00:18:31 -03:00
|
|
|
%(failfast)s%(catchbreak)s
|
2009-07-19 17:18:21 -03:00
|
|
|
Examples:
|
|
|
|
%(progName)s - run default set of tests
|
|
|
|
%(progName)s MyTestSuite - run suite 'MyTestSuite'
|
|
|
|
%(progName)s MyTestCase.testSomething - run MyTestCase.testSomething
|
|
|
|
%(progName)s MyTestCase - run all 'test*' test methods
|
|
|
|
in MyTestCase
|
|
|
|
"""
|
|
|
|
|
2010-03-26 00:18:31 -03:00
|
|
|
|
|
|
|
|
2009-07-19 17:18:21 -03:00
|
|
|
class TestProgram(object):
|
|
|
|
"""A command-line program that runs a set of tests; this is primarily
|
|
|
|
for making test modules conveniently executable.
|
|
|
|
"""
|
2010-03-21 23:49:08 -03:00
|
|
|
USAGE = USAGE_FROM_MODULE
|
2010-03-26 00:18:31 -03:00
|
|
|
|
|
|
|
# defaults for testing
|
|
|
|
failfast = catchbreak = None
|
|
|
|
|
2009-07-19 17:18:21 -03:00
|
|
|
def __init__(self, module='__main__', defaultTest=None,
|
|
|
|
argv=None, testRunner=None,
|
|
|
|
testLoader=loader.defaultTestLoader, exit=True,
|
2010-03-26 00:18:31 -03:00
|
|
|
verbosity=1, failfast=None, catchbreak=None):
|
2009-07-19 17:18:21 -03:00
|
|
|
if isinstance(module, basestring):
|
|
|
|
self.module = __import__(module)
|
|
|
|
for part in module.split('.')[1:]:
|
|
|
|
self.module = getattr(self.module, part)
|
|
|
|
else:
|
|
|
|
self.module = module
|
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv
|
|
|
|
|
|
|
|
self.exit = exit
|
2010-03-21 22:01:34 -03:00
|
|
|
self.failfast = failfast
|
2010-03-26 00:18:31 -03:00
|
|
|
self.catchbreak = catchbreak
|
2009-07-19 17:18:21 -03:00
|
|
|
self.verbosity = verbosity
|
|
|
|
self.defaultTest = defaultTest
|
|
|
|
self.testRunner = testRunner
|
|
|
|
self.testLoader = testLoader
|
|
|
|
self.progName = os.path.basename(argv[0])
|
|
|
|
self.parseArgs(argv)
|
|
|
|
self.runTests()
|
|
|
|
|
|
|
|
def usageExit(self, msg=None):
|
|
|
|
if msg:
|
|
|
|
print msg
|
2010-03-26 00:18:31 -03:00
|
|
|
usage = {'progName': self.progName, 'catchbreak': '', 'failfast': ''}
|
|
|
|
if self.failfast != False:
|
|
|
|
usage['failfast'] = FAILFAST
|
|
|
|
if self.catchbreak != False:
|
|
|
|
usage['catchbreak'] = CATCHBREAK
|
|
|
|
print self.USAGE % usage
|
2009-07-19 17:18:21 -03:00
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
def parseArgs(self, argv):
|
|
|
|
if len(argv) > 1 and argv[1].lower() == 'discover':
|
|
|
|
self._do_discovery(argv[2:])
|
|
|
|
return
|
|
|
|
|
|
|
|
import getopt
|
2010-03-26 00:18:31 -03:00
|
|
|
long_opts = ['help', 'verbose', 'quiet', 'failfast', 'catch']
|
2009-07-19 17:18:21 -03:00
|
|
|
try:
|
2010-03-26 00:18:31 -03:00
|
|
|
options, args = getopt.getopt(argv[1:], 'hHvqfc', long_opts)
|
2009-07-19 17:18:21 -03:00
|
|
|
for opt, value in options:
|
|
|
|
if opt in ('-h','-H','--help'):
|
|
|
|
self.usageExit()
|
|
|
|
if opt in ('-q','--quiet'):
|
|
|
|
self.verbosity = 0
|
|
|
|
if opt in ('-v','--verbose'):
|
|
|
|
self.verbosity = 2
|
2010-03-21 22:01:34 -03:00
|
|
|
if opt in ('-f','--failfast'):
|
2010-03-26 00:18:31 -03:00
|
|
|
if self.failfast is None:
|
|
|
|
self.failfast = True
|
|
|
|
# Should this raise an exception if -f is not valid?
|
|
|
|
if opt in ('-c','--catch'):
|
|
|
|
if self.catchbreak is None:
|
|
|
|
self.catchbreak = True
|
|
|
|
# Should this raise an exception if -c is not valid?
|
2009-07-19 17:18:21 -03:00
|
|
|
if len(args) == 0 and self.defaultTest is None:
|
2009-09-27 16:15:41 -03:00
|
|
|
# createTests will load tests from self.module
|
|
|
|
self.testNames = None
|
|
|
|
elif len(args) > 0:
|
2009-07-19 17:18:21 -03:00
|
|
|
self.testNames = args
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# to support python -m unittest ...
|
|
|
|
self.module = None
|
|
|
|
else:
|
|
|
|
self.testNames = (self.defaultTest,)
|
|
|
|
self.createTests()
|
|
|
|
except getopt.error, msg:
|
|
|
|
self.usageExit(msg)
|
|
|
|
|
|
|
|
def createTests(self):
|
2009-09-27 16:15:41 -03:00
|
|
|
if self.testNames is None:
|
|
|
|
self.test = self.testLoader.loadTestsFromModule(self.module)
|
|
|
|
else:
|
|
|
|
self.test = self.testLoader.loadTestsFromNames(self.testNames,
|
|
|
|
self.module)
|
2009-07-19 17:18:21 -03:00
|
|
|
|
|
|
|
def _do_discovery(self, argv, Loader=loader.TestLoader):
|
|
|
|
# handle command line args for test discovery
|
|
|
|
import optparse
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option('-v', '--verbose', dest='verbose', default=False,
|
|
|
|
help='Verbose output', action='store_true')
|
2010-03-26 00:18:31 -03:00
|
|
|
if self.failfast != False:
|
|
|
|
parser.add_option('-f', '--failfast', dest='failfast', default=False,
|
|
|
|
help='Stop on first fail or error',
|
|
|
|
action='store_true')
|
|
|
|
if self.catchbreak != False:
|
|
|
|
parser.add_option('-c', '--catch', dest='catchbreak', default=False,
|
|
|
|
help='Catch ctrl-C and display results so far',
|
|
|
|
action='store_true')
|
2009-07-19 17:18:21 -03:00
|
|
|
parser.add_option('-s', '--start-directory', dest='start', default='.',
|
|
|
|
help="Directory to start discovery ('.' default)")
|
|
|
|
parser.add_option('-p', '--pattern', dest='pattern', default='test*.py',
|
|
|
|
help="Pattern to match tests ('test*.py' default)")
|
|
|
|
parser.add_option('-t', '--top-level-directory', dest='top', default=None,
|
|
|
|
help='Top level directory of project (defaults to start directory)')
|
|
|
|
|
|
|
|
options, args = parser.parse_args(argv)
|
|
|
|
if len(args) > 3:
|
|
|
|
self.usageExit()
|
|
|
|
|
|
|
|
for name, value in zip(('start', 'pattern', 'top'), args):
|
|
|
|
setattr(options, name, value)
|
|
|
|
|
2010-03-26 00:18:31 -03:00
|
|
|
# only set options from the parsing here
|
|
|
|
# if they weren't set explicitly in the constructor
|
|
|
|
if self.failfast is None:
|
|
|
|
self.failfast = options.failfast
|
|
|
|
if self.catchbreak is None:
|
|
|
|
self.catchbreak = options.catchbreak
|
|
|
|
|
2009-07-19 17:18:21 -03:00
|
|
|
if options.verbose:
|
|
|
|
self.verbosity = 2
|
|
|
|
|
|
|
|
start_dir = options.start
|
|
|
|
pattern = options.pattern
|
|
|
|
top_level_dir = options.top
|
|
|
|
|
|
|
|
loader = Loader()
|
|
|
|
self.test = loader.discover(start_dir, pattern, top_level_dir)
|
|
|
|
|
|
|
|
def runTests(self):
|
2010-03-26 00:18:31 -03:00
|
|
|
if self.catchbreak:
|
|
|
|
installHandler()
|
2009-07-19 17:18:21 -03:00
|
|
|
if self.testRunner is None:
|
|
|
|
self.testRunner = runner.TextTestRunner
|
|
|
|
if isinstance(self.testRunner, (type, types.ClassType)):
|
|
|
|
try:
|
2010-03-21 22:01:34 -03:00
|
|
|
testRunner = self.testRunner(verbosity=self.verbosity,
|
|
|
|
failfast=self.failfast)
|
2009-07-19 17:18:21 -03:00
|
|
|
except TypeError:
|
2010-03-21 22:56:54 -03:00
|
|
|
# didn't accept the verbosity or failfast arguments
|
2009-07-19 17:18:21 -03:00
|
|
|
testRunner = self.testRunner()
|
|
|
|
else:
|
|
|
|
# it is assumed to be a TestRunner instance
|
|
|
|
testRunner = self.testRunner
|
|
|
|
self.result = testRunner.run(self.test)
|
|
|
|
if self.exit:
|
|
|
|
sys.exit(not self.result.wasSuccessful())
|
|
|
|
|
|
|
|
main = TestProgram
|