コード例 #1
0
def test_spec_completer(prefix, parsed_args, **kwargs):
    LOGGER.debug("TEST_SPEC_COMPLETER prefix='%s' kwargs=%r", prefix, kwargs)
    try:
        setup_top_level_directory(parsed_args.top_level_directory)
        completions = gen_test_spec_completion(prefix, parsed_args)
        for completion in completions:
            yield from with_next_completion(completion, parsed_args)
    except Exception as e:
        LOGGER.exception("uncaught exception!")
コード例 #2
0
ファイル: cli.py プロジェクト: nicolasdespres/hunittest
def main(argv):
    cli = build_cli()
    if ARGCOMPLETE_ENABLED:
        # It is tempting to set a validator that always return True so that
        # we could return the list of sub-modules without their full path
        # but unfortunately it does not work.
        argcomplete.autocomplete(cli)
    options = cli.parse_args(argv[1:])
    if options.version:
        print(get_version())
        return 0
    if options.pdb:
        options.njobs = 0
    top_level_directory = setup_top_level_directory(options.top_level_directory)
    filter_rules = options.filter_rules
    if filter_rules is None:
        filter_rules = FilterRules()
    failfast = options.failfast or options.pdb
    log_filename = get_log_filename()
    status_db = StatusDB(get_status_filename(options))
    result = None
    with LinePrinter(quiet=options.quiet,
                     color_mode=options.color,
                     isatty=True if options.tty else None,
                     verbose=options.verbose) as printer, \
         ResultPrinter(
             printer,
             top_level_directory,
             options.pattern,
             log_filename=log_filename,
             strip_unittest_traceback=options.strip_unittest_traceback,
             show_progress=not options.no_progress) as result_printer:
        # Collect only top level test specs (i.e. packages and modules, not test
        # classes and methods), because we have to tell the coverage package to
        # omit them. The omition could be done only when the reporting is
        # performed but the coveralls utility (who does its own reporting) won't
        # take it into account.
        if is_coverage_on(options):
            top_level_test_specs = reported_collect(
                printer,
                options.test_specs,
                options.pattern,
                filter_rules,
                top_level_directory,
                progress=not options.no_progress,
                top_level_only=True)
        else:
            top_level_test_specs = options.test_specs
        cov_args = dict(top_level_dir=top_level_directory,
                        reporters=options.coverage,
                        top_level_test_specs=top_level_test_specs)
        cov = CoverageInstrument(**cov_args)
        cov.erase()
        try:
            with cov:
                test_names = reported_collect(printer, options.test_specs,
                                              options.pattern, filter_rules,
                                              top_level_directory,
                                              progress=not options.no_progress)
            if options.collect_only or len(test_names) == 0:
                printer.new_line()
                return 0
            result_options = dict(
                result_printer=result_printer,
                total_tests=len(test_names),
                failfast=failfast,
                status_db=status_db,
            )
            with protect_cwd():
                if options.njobs == 0:
                    result = HTestResult(**result_options)
                    run_monoproc_tests(test_names, result, cov)
                else:
                    worker_kwargs = dict(
                        failfast=failfast
                    )
                    result = HTestResultServer(**result_options)
                    run_concurrent_tests(test_names, result,
                                         njobs=options.njobs,
                                         cov_args=cov_args,
                                         worker_kwargs=worker_kwargs)
            result.print_summary()
            printer.new_line()
        except Exception as e:
            printer.write_exception()
            return 2
        finally:
            cov.combine()
            if not options.collect_only:
                cov.report()
            if result is not None:
                write_error_test_specs(result)
    assert result is not None
    if result.wasSuccessful():
        return 0
    else:
        if is_pdb_on(options):
            if result.last_traceback is not None:
                printer.write_nl(">>> Entering pdb")
                # TODO(Nicolas Despres): Do not know how to properly skip
                #  the test runner traceback levels and to go up to the
                #  unit test case frame.
                #  See: http://pydoc.net/Python/django-pdb/0.4.1/django_pdb.testrunners/
                import pdb
                pdb.post_mortem(result.last_traceback)
        else:
            maybe_spawn_pager(options, log_filename,
                              False if options.tty else printer.isatty)
            # maybe_spawn_pager may never return if the pager has been
            # spawned. Otherwise we return 1.
        return 1