Source

unittest2 / unittest2 / main.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
"""Unittest main program"""

import optparse
import os
import sys
import types

from unittest2 import loader, runner, __version__
try:
    from unittest2.signals import installHandler
except ImportError:
    installHandler = None

from unittest2.config import getConfig
from unittest2.events import (
    loadPlugins, PluginsLoadedEvent,
    hooks, HandleFileEvent
)

__unittest = True

FAILFAST     = "  -f, --failfast   Stop on first failure\n"
CATCHBREAK   = "  -c, --catch      Catch control-C and display results\n"
BUFFEROUTPUT = "  -b, --buffer     Buffer stdout and stderr during test runs\n"

USAGE_AS_MAIN = """\
Usage: %(progName)s [options] [tests]

Options:
  -h, --help       Show this message
  -v, --verbose    Verbose output
  -q, --quiet      Minimal output
%(failfast)s%(catchbreak)s%(buffer)s
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
%(failfast)s%(catchbreak)s%(buffer)s  -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] [tests]

Options:
  -h, --help       Show this message
  -v, --verbose    Verbose output
  -q, --quiet      Minimal output
%(failfast)s%(catchbreak)s%(buffer)s
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
"""

DESCRIPTION = (
    '[tests] can be a list of any number of test modules, classes '
    'and test methods. The discover subcommand starts test discovery.'
)

# AttributeError only needed for Python 2.4
_OPT_ERRS = (optparse.BadOptionError, optparse.OptionValueError, AttributeError)

class _ImperviousOptionParser(optparse.OptionParser):
    def error(self, msg):
        pass
    def exit(self, status=0, msg=None):
        pass
    
    print_usage = print_version = print_help = lambda self, file=None: None

    def _process_short_opts(self, rargs, values):
        try:
            optparse.OptionParser._process_short_opts(self, rargs, values)
        except _OPT_ERRS:
            pass

    def _process_long_opt(self, rargs, values):
        try:
            optparse.OptionParser._process_long_opt(self, rargs, values)
        except _OPT_ERRS:
            pass


class _Callback(object):
    def __init__(self, callback):
        self.callback = callback
    def __call__(self, *_):
        self.callback()


class TestProgram(object):
    """A command-line program that runs a set of tests; this is primarily
       for making test modules conveniently executable.
    """
    USAGE = USAGE_FROM_MODULE
    
    # defaults for testing
    failfast = catchbreak = buffer = progName = module = defaultTest = None
    pluginsLoaded = verbosity = extraConfig = None

    def __init__(self, module='__main__', defaultTest=None,
                 argv=None, testRunner=None,
                 testLoader=loader.defaultTestLoader, exit=True,
                 verbosity=None, failfast=None, catchbreak=None, buffer=None,
                 config=None):
        if isinstance(module, basestring):
            __import__(module)
            self.module = sys.modules[module]
        else:
            self.module = module

        self.extraConfig = config
        if isinstance(verbosity, basestring):
            # allow string verbosities not in the dictionary
            # for backwards compatibility
            verbosity = runner.VERBOSITIES.get(verbosity.lower(), verbosity)

        if argv is None:
            argv = sys.argv

        self.exit = exit
        self.verbosity = verbosity
        self.failfast = failfast
        self.catchbreak = catchbreak
        self.buffer = buffer
        self.defaultTest = defaultTest
        self.testRunner = testRunner
        
        if isinstance(testLoader, type):
            testLoader = testLoader()
        self.testLoader = testLoader
        self.progName = os.path.basename(argv[0])

        self.parseArgs(argv)
        self.runTests()

    def usageExit(self, msg=None):
        if msg:
            print msg
        usage = {'progName': self.progName, 'catchbreak': '', 'failfast': '',
                 'buffer': ''}
        if self.failfast != False:
            usage['failfast'] = FAILFAST
        if self.catchbreak != False and installHandler is not None:
            usage['catchbreak'] = CATCHBREAK
        if self.buffer != False:
            usage['buffer'] = BUFFEROUTPUT
        print self.USAGE % usage
        sys.exit(2)

    def parseArgs(self, argv):
        forDiscovery = False
        if len(argv) > 1 and argv[1].lower() == 'discover':
            # handle command line args for test discovery
            self.progName = '%s discover' % self.progName
            argv = argv[1:]
            forDiscovery = True

        options, args = self._parseArgs(argv[1:], forDiscovery=forDiscovery)
        if (not forDiscovery and not args and self.module is None and 
            self.defaultTest is None):
            # launched with no args from script
            options.start = '.'
            options.top = options.pattern = None
            forDiscovery =True

        if forDiscovery:
            self._do_discovery(options, args)
            return
        
        if len(args) == 0 and self.defaultTest is None:
            # createTests will load tests from self.module
            self.testNames = None
        elif len(args) > 0:
            self.testNames = args
        else:
            self.testNames = (self.defaultTest,)
        
        self.createTests()

    def createTests(self):
        if self.testNames is None:
            self.test = self.testLoader.loadTestsFromModule(self.module)
        else:
            tests = []
            top_level = os.getcwd()
            loader = self.testLoader
            pattern = None
            # we could store the original and then restore after monkeypatch
            # probably not really an issue as this test runner is not intended
            # to be reused
            loader._top_level_dir = top_level
            
            for name in self.testNames:
                try:
                    test = self.testLoader.loadTestsFromName(name, self.module)
                except ImportError, e:
                    if not os.path.isfile(name):
                        raise
                    else:
                        path = os.path.split(name)[1]
                        event = HandleFileEvent(loader, path, name, pattern,
                                                top_level)
                        result = hooks.handleFile(event)
                        tests.extend(event.extraTests)
                        if event.handled:
                            tests.extend(result)
                            continue
                        
                        try:
                            name = loader._get_name_from_path(name)
                            test = self.testLoader.loadTestsFromName(name,
                                                                     self.module)
                        except (ImportError, AssertionError):
                            # better error message here perhaps?
                            raise e
                        tests.append(test)
                else:
                    tests.append(test)
                        
            self.test = self.testLoader.suiteClass(tests)

    def _getConfigOptions(self, argv):
        # an initial pass over command line options to load config files
        # and plugins
        parser = _ImperviousOptionParser()
        parser.add_option('--config', dest='configLocations', action='append')
        parser.add_option('--no-user-config', dest='noUserConfig', default=False,
                          action='store_true')
        parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
                          action='store_true')

        if TestProgram.pluginsLoaded:
            # only needed because we call several times during tests
            return False

        # we catch any optparse errors here as they will be
        # reraised on the second pass through
        try:
            options, _ = parser.parse_args(argv)
        except optparse.OptionError:
            pluginsDisabled = False
            noUserConfig = False
            configLocations = []
        else:
            pluginsDisabled = options.pluginsDisabled
            noUserConfig = options.noUserConfig
            configLocations = options.configLocations

        loadPlugins(pluginsDisabled, noUserConfig, configLocations,
                    self.extraConfig)
        TestProgram.pluginsLoaded = True
        return pluginsDisabled


    def _parseArgs(self, argv, forDiscovery):
        pluginsDisabled = self._getConfigOptions(argv)
        
        parser = optparse.OptionParser(version='unittest2 %s' % __version__)

        if forDiscovery:
            parser.usage = '%prog [options] [...]'
        else:
            parser.description = DESCRIPTION
            parser.usage = '%prog [options] [tests]'
        parser.prog = self.progName
        parser.add_option('-v', '--verbose', dest='verbose', default=False,
                          help='Verbose output', action='store_true')
        parser.add_option('-q', '--quiet', dest='quiet', default=False,
                          help='Quiet output', action='store_true')
        
        parser.add_option('--config', dest='configLocations', action='append',
                          help='Specify local config file location')
        parser.add_option('--no-user-config', dest='noUserConfig', default=False,
                          action='store_true',
                          help="Don't use user config file")
        parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
                          action='store_true', help="Disable all plugins")
        
        if self.failfast != False:
            parser.add_option('-f', '--failfast', dest='failfast', default=None,
                              help='Stop on first fail or error', 
                              action='store_true')
        if self.catchbreak != False and installHandler is not None:
            parser.add_option('-c', '--catch', dest='catchbreak', default=None,
                              help='Catch ctrl-C and display results so far', 
                              action='store_true')
        if self.buffer != False:
            parser.add_option('-b', '--buffer', dest='buffer', default=None,
                              help='Buffer stdout and stderr during tests', 
                              action='store_true')

        if forDiscovery:
            parser.add_option('-s', '--start-directory', dest='start', default='.',
                              help="Directory to start discovery ('.' default)")
            parser.add_option('-p', '--pattern', dest='pattern', default=None,
                              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)')

        list_options = []

        if not pluginsDisabled:
            extra_options = []
            if forDiscovery:
                extra_options = _discoveryOptions
            for opt, longopt, help_text, callback in _options + extra_options:
                opts = []
                if opt is not None:
                    opts.append('-' + opt)
                if longopt is not None:
                    opts.append('--' + longopt)
                kwargs = dict(
                    action='callback',
                    help=help_text,
                )
                if isinstance(callback, list):
                    kwargs['action'] = 'append'
                    kwargs['dest'] = longopt
                    list_options.append((longopt, callback))
                else:
                    kwargs['callback'] = _Callback(callback)
                option = optparse.make_option(*opts, **kwargs)
                parser.add_option(option)

        options, args = parser.parse_args(argv)

        for attr, _list in list_options:
            values = getattr(options, attr) or []
            _list.extend(values)

        config = getConfig('unittest')
        if self.verbosity is None:
            try:
                self.verbosity = config.as_int('verbosity', 1)
            except ValueError:
                if ('verbosity' in config and 
                    config['verbosity'].lower() in runner.VERBOSITIES):
                    self.verbosity = runner.VERBOSITIES[config['verbosity']]
                else:
                    raise

        config['discover'] = forDiscovery

        if self.buffer is not None:
            options.buffer = self.buffer
        if self.failfast is not None:
            options.failfast = self.failfast
        if installHandler is None:
            options.catchbreak = False
        elif self.catchbreak is not None:
            options.catchbreak = self.catchbreak

        if options.buffer is None:
            options.buffer = config.as_bool('buffer', default=False)
        if options.failfast is None:
            options.failfast = config.as_bool('failfast', default=False)
        if options.catchbreak is None:
            options.catchbreak = config.as_bool('catch', default=False)

        self.failfast = options.failfast
        self.buffer = options.buffer
        self.catchbreak = options.catchbreak

        if options.verbose:
            self.verbosity = 2
        if options.quiet:
            self.verbosity = 0
        if options.quiet and options.verbose:
            # could raise an exception here I suppose
            self.verbosity = 1

        config['verbosity'] = self.verbosity
        config['buffer'] = self.buffer
        config['catch'] = self.catchbreak
        config['failfast'] = self.failfast

        hooks.pluginsLoaded(PluginsLoadedEvent())
        return options, args

    def _do_discovery(self, options, args):
        if len(args) > 3:
            self.usageExit()
        
        for name, value in zip(('start', 'pattern', 'top'), args):
            setattr(options, name, value)

        start_dir = options.start
        pattern = options.pattern
        top_level_dir = options.top

        loader = self.testLoader
        self.test = loader.discover(start_dir, pattern, top_level_dir)

    def runTests(self):
        if self.catchbreak:
            installHandler()
        if self.testRunner is None:
            self.testRunner = runner.TextTestRunner
        if isinstance(self.testRunner, (type, types.ClassType)):
            try:
                testRunner = self.testRunner(verbosity=self.verbosity,
                                             failfast=self.failfast,
                                             buffer=self.buffer)
            except TypeError:
                # didn't accept the verbosity, buffer or failfast arguments
                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())

    

def main_():
    TestProgram.USAGE = USAGE_AS_MAIN
    TestProgram(module=None)

_options = []
_discoveryOptions = []