2009-07-19 17:18:21 -03:00
|
|
|
"""Unittest main program"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import types
|
|
|
|
|
|
|
|
from . import loader, runner
|
|
|
|
|
2010-03-21 21:06:30 -03:00
|
|
|
__unittest = True
|
|
|
|
|
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-21 22:01:34 -03:00
|
|
|
-f, --failfast Stop on first failure
|
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-21 22:01:34 -03:00
|
|
|
-f, --failfast Stop on first failure
|
2009-07-19 17:18:21 -03:00
|
|
|
-s directory Directory to start discovery ('.' default)
|
|
|
|
-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-21 22:01:34 -03:00
|
|
|
-f, --failfast Stop on first failure
|
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
|
|
|
|
"""
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
USAGE = USAGE_AS_MAIN
|
|
|
|
else:
|
|
|
|
USAGE = USAGE_FROM_MODULE
|
|
|
|
|
|
|
|
|
|
|
|
class TestProgram(object):
|
|
|
|
"""A command-line program that runs a set of tests; this is primarily
|
|
|
|
for making test modules conveniently executable.
|
|
|
|
"""
|
|
|
|
USAGE = USAGE
|
|
|
|
def __init__(self, module='__main__', defaultTest=None,
|
|
|
|
argv=None, testRunner=None,
|
|
|
|
testLoader=loader.defaultTestLoader, exit=True,
|
2010-03-21 22:01:34 -03:00
|
|
|
verbosity=1, failfast=False):
|
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
|
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
|
|
|
|
print self.USAGE % self.__dict__
|
|
|
|
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-21 22:01:34 -03:00
|
|
|
long_opts = ['help', 'verbose', 'quiet', 'failfast']
|
2009-07-19 17:18:21 -03:00
|
|
|
try:
|
2010-03-21 22:01:34 -03:00
|
|
|
options, args = getopt.getopt(argv[1:], 'hHvqf', 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'):
|
|
|
|
self.failfast = True
|
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-21 22:01:34 -03:00
|
|
|
parser.add_option('-f', '--failfast', dest='failfast', default=False,
|
|
|
|
help='Stop on first fail or error', 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)
|
|
|
|
|
|
|
|
if options.verbose:
|
|
|
|
self.verbosity = 2
|
2010-03-21 22:01:34 -03:00
|
|
|
if options.failfast:
|
|
|
|
self.failfast = True
|
2009-07-19 17:18:21 -03:00
|
|
|
|
|
|
|
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):
|
|
|
|
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:01:34 -03:00
|
|
|
# didn't accept the verbosity or failfast argument
|
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
|