Beispiel #1
0
    def run(self):
        args = self.args
        location = args.location

        console = self.console
        if args.output is not None:
            from moya.console import Console
            console = Console(html=True)

        def write_output():
            if args.output is not None:
                from moya import consolehtml
                html = console.get_text()
                html = consolehtml.render_console_html(html)
                with open(args.output, 'wb') as f:
                    f.write(html.encode('utf-8'))

        test_libs = []
        for location in args.location:
            archive, lib = build.build_lib(location, tests=True)
            lib_name = lib.long_name

            if archive.failed_documents:
                sys.stderr.write('library build failed\n')
                build.render_failed_documents(archive, console)
                write_output()
                return -1

            test_libs.append(lib)

        for lib_name in test_libs:
            if not lib.load_tests():
                sys.stderr.write('no tests for {}\n'.format(lib))
                return -1

        archive.finalize()

        if archive.failed_documents:
            sys.stderr.write('tests build failed\n')
            build.render_failed_documents(archive, console)
            write_output()
            return -1

        if args.automated:
            archive.suppress_breakpoints = True

        all_results = []

        for lib in test_libs:
            suites = list(lib.get_elements_by_type((namespaces.test, 'suite')))

            for suite_no, suite in enumerate(suites, 1):

                try:
                    suite_description = suite.description
                except:
                    suite_description = suite.libid

                if args.quick and suite.slow:
                    with self.console.progress("{} {} (skipped slow test)".format(lib, suite_description), 0):
                        pass
                    continue

                if args.exclude and suite.group is not None and suite.group in args.exclude:
                    with self.console.progress("{} {} (excluded group)".format(lib, suite_description), 0):
                        pass
                    continue

                if args.group and suite.group not in args.group:
                    with self.console.progress("{} {} (not in group)".format(lib, suite_description), 0):
                        pass
                    continue

                steps = 0
                setup = suite.get_child((namespaces.test, 'setup'))
                teardown = suite.get_child((namespaces.test, 'teardown'))
                if setup is not None:
                    setup_callable = archive.get_callable_from_element(setup)
                    steps += 1
                if teardown is not None:
                    teardown_callable = archive.get_callable_from_element(teardown)
                    steps += 1
                tests = list(suite.children((namespaces.test, 'case')))
                steps += len(tests)

                test_runner = {}
                context = Context({'_test_runner': test_runner})

                results = TestResults(context, lib, suite.description)
                results.group = suite._definition
                all_results.append(results)

                context['._test_results'] = results
                test_info = "({} of {})".format(suite_no, len(suites))
                progress_text = "{} {} {}".format(lib, suite_description, test_info)

                with self.console.progress(progress_text, steps) as progress:
                    if steps == 0:
                        progress.step()
                    else:
                        try:
                            with TestRunner(context, results):
                                if setup is not None:
                                    setup_callable(context)
                                    progress.step()
                        except Exception as e:
                            results.add_setup_error(setup, e)
                            progress.step('setup failed')
                        else:
                            with TestRunner(context, results):
                                for test in tests:
                                    results.case = test
                                    test_callable = archive.get_callable_from_element(test)
                                    try:
                                        test_return = test_callable(context)
                                    except Exception as e:
                                        results.add_error(test, e)
                                    else:
                                        if test_return != 'fail':
                                            results.add_pass(test)
                                    finally:
                                        progress.step()

                            try:
                                with TestRunner(context, results):
                                    if teardown is not None:
                                        teardown_callable(context)
                                        progress.step()
                            except Exception as e:
                                results.add_teardown_error(teardown, e)

        all_totals = {'pass': 0, 'fail': 0, 'error': 0}
        for result in all_results:
            _pass, fail, error = result.stats
            all_totals['pass'] += _pass
            all_totals['fail'] += fail
            all_totals['error'] += error
        summary = "{fail} fail(s), {error} error(s), {pass} pass(es)".format(**all_totals)

        if args.quick:
            console.div("Test results (quick) {}".format(datetime.now().ctime()))
        else:
            console.div("Test results {}".format(datetime.now().ctime()))
        test_count = sum(len(r.tests) for r in all_results)
        console.text('Ran {} test(s) in {} test suite(s) - {}'.format(test_count, len(all_results), summary))
        for results in all_results:
            results.report(console,
                           show_trace=not args.summary,
                           show_passes=args.verbose or args.summary,
                           verbose=args.verbose)

        header = ['lib', 'suite', 'passes', 'fails', 'errors']
        table = []
        passes = 0
        fails = 0
        errors = 0
        for result in all_results:
            _pass, fail, error = result.stats
            passes += _pass
            fails += fail
            errors += error
            _pass = Cell(_pass, fg="green" if _pass else 'white', bold=True)
            fail = Cell(fail, fg="red" if fail else "green", bold=True)
            error = Cell(error, fg="red" if error else "green", bold=True)
            table.append([result.lib.long_name, result.suite, _pass, fail, error])

        if not args.summary or args.verbose:
            console.nl()
            console.table(table, header_row=header)

        summary = "{fails} fail(s), {errors} error(s), {passes} pass(es)".format(fails=fails, errors=errors, passes=passes)
        if errors or fails:
            console.text(summary, fg="red", bold=True)
        else:
            console.text(summary, fg="green", bold=True)

        write_output()

        return fails
Beispiel #2
0
    def run(self):
        args = self.args
        location = args.location

        console = self.console
        if args.output is not None:
            from moya.console import Console
            console = Console(html=True)

        def write_output():
            if args.output is not None:
                from moya import consolehtml
                html = console.get_text()
                html = consolehtml.render_console_html(html)
                with open(args.output, 'wb') as f:
                    f.write(html.encode('utf-8'))

        test_libs = []
        for location in args.location:
            archive, lib = build.build_lib(location)
            lib_name = lib.long_name

            if archive.failed_documents:
                sys.stderr.write('library build failed\n')
                build.render_failed_documents(archive, console)
                write_output()
                return -1

            test_libs.append(lib)

        for lib_name in test_libs:
            if not lib.load_tests():
                sys.stderr.write('no tests for {}\n'.format(lib))
                return -1

        archive.finalize()

        if archive.failed_documents:
            sys.stderr.write('tests build failed\n')
            build.render_failed_documents(archive, console)
            write_output()
            return -1

        if args.automated:
            archive.suppress_breakpoints = True

        all_results = []

        for lib in test_libs:
            suites = list(lib.get_elements_by_type((namespaces.test, 'suite')))

            for suite_no, suite in enumerate(suites, 1):

                try:
                    suite_description = suite.description
                except:
                    suite_description = suite.libid

                if args.quick and suite.slow:
                    with self.console.progress("{} {} (skipped slow test)".format(lib, suite_description), 0):
                        pass
                    continue

                if args.exclude and suite.group is not None and suite.group in args.exclude:
                    with self.console.progress("{} {} (excluded group)".format(lib, suite_description), 0):
                        pass
                    continue

                if args.group and suite.group not in args.group:
                    with self.console.progress("{} {} (not in group)".format(lib, suite_description), 0):
                        pass
                    continue

                steps = 0
                setup = suite.get_child((namespaces.test, 'setup'))
                teardown = suite.get_child((namespaces.test, 'teardown'))
                if setup is not None:
                    setup_callable = archive.get_callable_from_element(setup)
                    steps += 1
                if teardown is not None:
                    teardown_callable = archive.get_callable_from_element(teardown)
                    steps += 1
                tests = list(suite.children((namespaces.test, 'case')))
                steps += len(tests)

                #test_runner = {"break": args._break}
                test_runner = {}
                context = Context({'_test_runner': test_runner})

                results = TestResults(context, lib, suite.description)
                results.group = suite._definition
                all_results.append(results)

                context['._test_results'] = results
                test_info = "({} of {})".format(suite_no, len(suites))
                progress_text = "{} {} {}".format(lib, suite_description, test_info)

                with self.console.progress(progress_text, steps) as progress:
                    if steps == 0:
                        progress.step()
                    else:
                        try:
                            with TestRunner(context, results):
                                if setup is not None:
                                    setup_callable(context)
                                    progress.step()
                        except Exception as e:
                            results.add_setup_error(setup, e)
                            progress.step('setup failed')
                        else:
                            with TestRunner(context, results):
                                for test in tests:
                                    results.case = test
                                    test_callable = archive.get_callable_from_element(test)
                                    try:
                                        test_return = test_callable(context)
                                    except Exception as e:
                                        results.add_error(test, e)
                                    else:
                                        if test_return != 'fail':
                                            results.add_pass(test)
                                    finally:
                                        progress.step()

                            try:
                                with TestRunner(context, results):
                                    if teardown is not None:
                                        teardown_callable(context)
                                        progress.step()
                            except Exception as e:
                                results.add_teardown_error(teardown, e)

        all_totals = {'pass': 0, 'fail': 0, 'error': 0}
        for result in all_results:
            _pass, fail, error = result.stats
            all_totals['pass'] += _pass
            all_totals['fail'] += fail
            all_totals['error'] += error
        summary = "{fail} fail(s), {error} error(s), {pass} pass(es)".format(**all_totals)

        #self.console.nl()
        if args.quick:
            console.div("Test results (quick) {}".format(datetime.now().ctime()))
        else:
            console.div("Test results {}".format(datetime.now().ctime()))
        test_count = sum(len(r.tests) for r in all_results)
        console.text('Ran {} test(s) in {} test suite(s) - {}'.format(test_count, len(all_results), summary))
        for results in all_results:
            results.report(console,
                           show_trace=not args.summary,
                           show_passes=args.verbose or args.summary,
                           verbose=args.verbose)

        header = ['lib', 'suite', 'passes', 'fails', 'errors']
        table = []
        passes = 0
        fails = 0
        errors = 0
        for result in all_results:
            _pass, fail, error = result.stats
            passes += _pass
            fails += fail
            errors += error
            _pass = Cell(_pass, fg="green" if _pass else 'white', bold=True)
            fail = Cell(fail, fg="red" if fail else "green", bold=True)
            error = Cell(error, fg="red" if error else "green", bold=True)
            table.append([result.lib.long_name, result.suite, _pass, fail, error])

        if not args.summary or args.verbose:
            console.nl()
            console.table(table, header_row=header)

        summary = "{fails} fail(s), {errors} error(s), {passes} pass(es)".format(fails=fails, errors=errors, passes=passes)
        if errors or fails:
            console.text(summary, fg="red", bold=True)
        else:
            console.text(summary, fg="green", bold=True)

        write_output()

        return fails