Skip to content
This repository
tree: 9b464ff95a
Fetching contributors…

Cannot retrieve contributors at this time

file 118 lines (102 sloc) 3.852 kb
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
from __future__ import with_statement

import sys
import os
from os import path

from pkg_resources import get_distribution
from optparse import OptionParser, make_option
from attest.collectors import Tests
from attest.reporters import get_all_reporters, get_reporter_by_name
from attest.utils import parse_options
from attest.hook import AssertImportHook


def make_parser(**kwargs):
    args = dict(
        prog='attest',
        usage='%prog [options] [tests...] [key=value...]',
        version=get_distribution('Attest').version,

        description=(
            'The positional "tests" are dotted '
            'names for modules or packages that are scanned '
            'recursively for Tests instances, or dotted names '
            'for any other object that iterates over tests. If '
            'not provided, packages in the working directory '
            'are scanned.\n'
            'The key/value pairs are passed to the '
            'reporter constructor, after some command-line '
            'friendly parsing.'
        ),

        option_list=[
            make_option('-d', '--debugger',
                action='store_true',
                help='enter pdb for failing tests',
            ),
            make_option('-r', '--reporter',
                metavar='NAME',
                help='select reporter by name'
            ),
            make_option('-l', '--list-reporters',
                action='store_true',
                help='list available reporters'
            ),
            make_option('-n', '--no-capture',
                action='store_true',
                help="don't capture stderr and stdout"
            ),
            make_option('--full-tracebacks',
                action='store_true',
                help="don't clean tracebacks"
            ),
            make_option('--fail-fast',
                action='store_true',
                help='stop at first failure'
            ),
            make_option('--native-assert',
                action='store_true',
                help="don't hook the assert statement"
            ),
            make_option('-p', '--profile',
                metavar='FILENAME',
                help='enable tests profiling and store results in filename'
            ),
            make_option('-k', '--keyboard-interrupt',
                action='store_true',
                help="Let KeyboardInterrupt exceptions (CTRL+C) propagate"
            ),
        ]
    )
    args.update(kwargs)
    return OptionParser(**args)


def main(tests=None, **kwargs):
    parser = make_parser(**kwargs)
    options, args = parser.parse_args()

    if options.list_reporters:
        for reporter in get_all_reporters():
            print reporter
        return

    opts = parse_options(args)
    reporter = get_reporter_by_name(options.reporter)(**opts)

    if not tests:
        sys.path.insert(0, os.getcwd())
        names = [arg for arg in args if '=' not in arg]
        if not names:
            names = [name for name in os.listdir('.')
                          if path.isfile('%s/__init__.py' % name)]

        if options.native_assert:
            tests = Tests(names)
        else:
            with AssertImportHook():
                tests = Tests(names)

    def run():
        tests.run(reporter, full_tracebacks=options.full_tracebacks,
                            fail_fast=options.fail_fast,
                            debugger=options.debugger,
                            no_capture=options.no_capture,
                            keyboard_interrupt=options.keyboard_interrupt)

    if options.profile:
        filename = options.profile
        import cProfile
        cProfile.runctx('run()', globals(), locals(), filename)
        print 'Wrote profiling results to %r.' % (filename,)
    else:
        run()


if __name__ == '__main__':
    main()
Something went wrong with that request. Please try again.