Esempio n. 1
0
    def console_writer_before_each_step(self, step):
        """
            Writes the step to the console before it is run

            :param Step step: the step to write to the console
        """
        if not isinstance(step.parent.parent, Feature):
            return

        if world.config.write_steps_once:
            return

        output = ""
        if step.as_precondition and self.last_precondition != step.as_precondition:
            output += colorful.white(
                u"      As precondition from {0}: {1}\n".format(
                    os.path.basename(step.as_precondition.path),
                    step.as_precondition.sentence))
        elif not step.as_precondition and self.last_precondition:
            output += colorful.white(u"      From scenario\n")

        self.last_precondition = step.as_precondition
        output += u"\r        {0}{1}".format(
            self.get_id_sentence_prefix(step, colorful.bold_brown),
            colorful.bold_brown(step.sentence))

        if step.text:
            id_padding = self.get_id_padding(len(step.parent.steps))
            output += colorful.bold_white(
                u'\n            {0}"""'.format(id_padding))
            output += colorful.cyan(u"".join([
                "\n                {0}{1}".format(id_padding, l)
                for l in step.raw_text
            ]))
            output += colorful.bold_white(
                u'\n            {0}"""'.format(id_padding))

        if step.table:
            colored_pipe = colorful.bold_white("|")
            col_widths = self.get_table_col_widths(step.table)
            for row in step.table:
                output += u"\n          {0} {1} {0}".format(
                    colored_pipe, (" {0} ").format(colored_pipe).join(
                        colorful.bold_brown(u"{1: <{0}}".format(
                            col_widths[i], x)) for i, x in enumerate(row)))

        write(output)
Esempio n. 2
0
    def console_writer_before_each_scenario(self, scenario):
        """
            Writes the scenario header to the console

            :param Scenario scenario: the scenario to write to the console
        """
        output = "\n"
        if isinstance(scenario.parent, ScenarioOutline):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(
                scenario, colorful.bold_brown, len(scenario.parent.scenarios))
            colored_pipe = colorful.bold_white("|")
            output = u"        {0}{1} {2} {1}".format(
                id_prefix, colored_pipe, (u" {0} ").format(colored_pipe).join(
                    colorful.bold_brown(u"{1: <{0}}".format(
                        scenario.parent.get_column_width(i), x))
                    for i, x in enumerate(scenario.example.data)))
        elif isinstance(scenario.parent, ScenarioLoop):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(
                scenario, colorful.bold_brown, len(scenario.parent.scenarios))
            colored_pipe = colorful.bold_white("|")
            output = u"        {0}{1} {2: <18} {1}".format(
                id_prefix, colored_pipe,
                colorful.bold_brown(scenario.iteration))
        else:
            id_prefix = self.get_id_sentence_prefix(scenario,
                                                    colorful.bold_cyan)
            for tag in scenario.tags:
                if tag.name == "precondition" and world.config.expand and world.config.show:  # exceptional for show command when scenario steps expand and tag is a precondition -> comment it out
                    output += colorful.white(u"    # @{0}{1}\n".format(
                        tag.name, "({0})".format(tag.arg) if tag.arg else ""))
                else:
                    output += colorful.cyan(u"    @{0}{1}\n".format(
                        tag.name, u"({0})".format(tag.arg) if tag.arg else ""))
            output += u"    {0}{1}: {2}".format(
                id_prefix, colorful.bold_white(scenario.keyword),
                colorful.bold_white(scenario.sentence))
        write(output)
Esempio n. 3
0
    def console_writer_before_each_scenario(self, scenario):
        """
            Writes the scenario header to the console

            :param Scenario scenario: the scenario to write to the console
        """
        output = "\n"
        if isinstance(scenario.parent, ScenarioOutline):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(scenario, colorful.bold_brown, len(scenario.parent.scenarios))
            colored_pipe = colorful.bold_white("|")
            output = u"        {0}{1} {2} {1}".format(
                id_prefix,
                colored_pipe,
                (u" {0} ").format(colored_pipe).join(
                    colorful.bold_brown(u"{1: <{0}}".format(scenario.parent.get_column_width(i), x)) for i, x in enumerate(scenario.example.data)
                )
            )
        elif isinstance(scenario.parent, ScenarioLoop):
            if world.config.write_steps_once:
                return

            id_prefix = self.get_id_sentence_prefix(scenario, colorful.bold_brown, len(scenario.parent.scenarios))
            colored_pipe = colorful.bold_white("|")
            output = u"        {0}{1} {2: <18} {1}".format(id_prefix, colored_pipe, colorful.bold_brown(scenario.iteration))
        else:
            id_prefix = self.get_id_sentence_prefix(scenario, colorful.bold_cyan)
            for tag in scenario.tags:
                if tag.name == "precondition" and world.config.expand and world.config.show:  # exceptional for show command when scenario steps expand and tag is a precondition -> comment it out
                    output += colorful.white(u"    # @{0}{1}\n".format(tag.name, "({0})".format(tag.arg) if tag.arg else ""))
                else:
                    output += colorful.cyan(u"    @{0}{1}\n".format(tag.name, u"({0})".format(tag.arg) if tag.arg else ""))
            output += u"    {0}{1}: {2}".format(id_prefix, colorful.bold_white(scenario.keyword), colorful.bold_white(scenario.sentence))
        write(output)
Esempio n. 4
0
    def console_writer_before_each_step(self, step):
        """
            Writes the step to the console before it is run

            :param Step step: the step to write to the console
        """
        if not isinstance(step.parent.parent, Feature):
            return

        if world.config.write_steps_once:
            return

        output = ""
        if step.as_precondition and self.last_precondition != step.as_precondition:
            output += colorful.white(u"      As precondition from {0}: {1}\n".format(os.path.basename(step.as_precondition.path), step.as_precondition.sentence))
        elif not step.as_precondition and self.last_precondition:
            output += colorful.white(u"      From scenario\n")

        self.last_precondition = step.as_precondition
        output += u"\r        {0}{1}".format(self.get_id_sentence_prefix(step, colorful.bold_brown), colorful.bold_brown(step.sentence))

        if step.text:
            id_padding = self.get_id_padding(len(step.parent.steps))
            output += colorful.bold_white(u'\n            {0}"""'.format(id_padding))
            output += colorful.cyan(u"".join(["\n                {0}{1}".format(id_padding, l) for l in step.raw_text]))
            output += colorful.bold_white(u'\n            {0}"""'.format(id_padding))

        if step.table:
            colored_pipe = colorful.bold_white("|")
            col_widths = self.get_table_col_widths(step.table)
            for row in step.table:
                output += u"\n          {0} {1} {0}".format(colored_pipe, (" {0} ").format(colored_pipe).join(
                    colorful.bold_brown(u"{1: <{0}}".format(col_widths[i], x)) for i, x in enumerate(row)
                ))

        write(output)
Esempio n. 5
0
    def console_write(self, features, marker):
        """
            Writes the endreport for all features

            :param list features: all features
        """
        stats = {
            "features": {
                "amount": 0,
                "passed": 0,
                "failed": 0,
                "skipped": 0,
                "untested": 0,
                "pending": 0
            },
            "scenarios": {
                "amount": 0,
                "passed": 0,
                "failed": 0,
                "skipped": 0,
                "untested": 0,
                "pending": 0
            },
            "steps": {
                "amount": 0,
                "passed": 0,
                "failed": 0,
                "skipped": 0,
                "untested": 0,
                "pending": 0
            },
        }
        pending_steps = []
        duration = timedelta()
        for feature in features:
            if not feature.has_to_run(world.config.scenarios,
                                      world.config.feature_tags,
                                      world.config.scenario_tags):
                continue
            stats["features"]["amount"] += 1
            stats["features"][feature.state] += 1

            if feature.state in [Step.State.PASSED, Step.State.FAILED]:
                duration += feature.duration

            for scenario in feature.all_scenarios:
                if not scenario.has_to_run(world.config.scenarios,
                                           world.config.feature_tags,
                                           world.config.scenario_tags):
                    continue

                if isinstance(scenario,
                              ScenarioOutline):  # skip ScenarioOutlines
                    continue
                if isinstance(scenario, ScenarioLoop):  # skip ScenarioLoop
                    continue

                stats["scenarios"]["amount"] += 1
                stats["scenarios"][scenario.state] += 1
                for step in scenario.steps:
                    stats["steps"]["amount"] += 1
                    stats["steps"][step.state] += 1

                    if step.state == Step.State.PENDING:
                        pending_steps.append(step)

        colored_closing_paren = colorful.bold_white(")")
        colored_comma = colorful.bold_white(", ")
        passed_word = colorful.bold_green("{0} passed")
        failed_word = colorful.bold_red("{0} failed")
        skipped_word = colorful.cyan("{0} skipped")
        pending_word = colorful.bold_brown("{0} pending")

        output = colorful.bold_white("{0} features (".format(
            stats["features"]["amount"]))
        output += passed_word.format(stats["features"]["passed"])
        if stats["features"]["failed"]:
            output += colored_comma + failed_word.format(
                stats["features"]["failed"])
        if stats["features"]["skipped"]:
            output += colored_comma + skipped_word.format(
                stats["features"]["skipped"])
        if stats["features"]["pending"]:
            output += colored_comma + pending_word.format(
                stats["features"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white("{} scenarios (".format(
            stats["scenarios"]["amount"]))
        output += passed_word.format(stats["scenarios"]["passed"])
        if stats["scenarios"]["failed"]:
            output += colored_comma + failed_word.format(
                stats["scenarios"]["failed"])
        if stats["scenarios"]["skipped"]:
            output += colored_comma + skipped_word.format(
                stats["scenarios"]["skipped"])
        if stats["scenarios"]["pending"]:
            output += colored_comma + pending_word.format(
                stats["scenarios"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white("{} steps (".format(
            stats["steps"]["amount"]))
        output += passed_word.format(stats["steps"]["passed"])
        if stats["steps"]["failed"]:
            output += colored_comma + failed_word.format(
                stats["steps"]["failed"])
        if stats["steps"]["skipped"]:
            output += colored_comma + skipped_word.format(
                stats["steps"]["skipped"])
        if stats["steps"]["pending"]:
            output += colored_comma + pending_word.format(
                stats["steps"]["pending"])
        output += colored_closing_paren

        if pending_steps:
            sr = StepRegistry()
            pending_step_implementations = make_unique_obj_list(
                pending_steps, lambda x: x.definition_func)
            output += colorful.white(
                "\nYou have {0} pending step implementation{1} affecting {2} step{3}:\n  {4}\n\nNote: this could be the reason for some failing subsequent steps"
                .format(
                    len(pending_step_implementations),
                    "s" if len(pending_step_implementations) is not 1 else "",
                    len(pending_steps),
                    "s" if len(pending_steps) is not 1 else "", "\n  ".join([
                        "-  '{0}' @ {1}".format(
                            sr.get_pattern(s.definition_func),
                            get_func_code(s.definition_func).co_filename)
                        for s in pending_step_implementations
                    ])))

        output += "\n"
        output += colorful.cyan(
            "Run {0} finished within {1}:{2} minutes".format(
                marker,
                int(duration.total_seconds()) / 60,
                duration.total_seconds() % 60.0))

        write(output)
Esempio n. 6
0
    def console_write(self, features, marker):
        """
            Writes the endreport for all features

            :param list features: all features
        """
        stats = {
            "features": {"amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0},
            "scenarios": {"amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0},
            "steps": {"amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0},
        }
        pending_steps = []
        duration = timedelta()
        for feature in features:
            if not feature.has_to_run(world.config.scenarios, world.config.feature_tags, world.config.scenario_tags):
                continue
            stats["features"]["amount"] += 1
            stats["features"][feature.state] += 1

            if feature.state in [Step.State.PASSED, Step.State.FAILED]:
                duration += feature.duration

            for scenario in feature.all_scenarios:
                if not scenario.has_to_run(world.config.scenarios, world.config.feature_tags, world.config.scenario_tags):
                    continue

                if isinstance(scenario, ScenarioOutline):  # skip ScenarioOutlines
                    continue
                if isinstance(scenario, ScenarioLoop):  # skip ScenarioLoop
                    continue

                stats["scenarios"]["amount"] += 1
                stats["scenarios"][scenario.state] += 1
                for step in scenario.steps:
                    stats["steps"]["amount"] += 1
                    stats["steps"][step.state] += 1

                    if step.state == Step.State.PENDING:
                        pending_steps.append(step)

        colored_closing_paren = colorful.bold_white(")")
        colored_comma = colorful.bold_white(", ")
        passed_word = colorful.bold_green("{0} passed")
        failed_word = colorful.bold_red("{0} failed")
        skipped_word = colorful.cyan("{0} skipped")
        pending_word = colorful.bold_brown("{0} pending")

        output = colorful.bold_white("{0} features (".format(stats["features"]["amount"]))
        output += passed_word.format(stats["features"]["passed"])
        if stats["features"]["failed"]:
            output += colored_comma + failed_word.format(stats["features"]["failed"])
        if stats["features"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["features"]["skipped"])
        if stats["features"]["pending"]:
            output += colored_comma + pending_word.format(stats["features"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white("{} scenarios (".format(stats["scenarios"]["amount"]))
        output += passed_word.format(stats["scenarios"]["passed"])
        if stats["scenarios"]["failed"]:
            output += colored_comma + failed_word.format(stats["scenarios"]["failed"])
        if stats["scenarios"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["scenarios"]["skipped"])
        if stats["scenarios"]["pending"]:
            output += colored_comma + pending_word.format(stats["scenarios"]["pending"])
        output += colored_closing_paren

        output += "\n"
        output += colorful.bold_white("{} steps (".format(stats["steps"]["amount"]))
        output += passed_word.format(stats["steps"]["passed"])
        if stats["steps"]["failed"]:
            output += colored_comma + failed_word.format(stats["steps"]["failed"])
        if stats["steps"]["skipped"]:
            output += colored_comma + skipped_word.format(stats["steps"]["skipped"])
        if stats["steps"]["pending"]:
            output += colored_comma + pending_word.format(stats["steps"]["pending"])
        output += colored_closing_paren

        if pending_steps:
            sr = StepRegistry()
            pending_step_implementations = make_unique_obj_list(pending_steps, lambda x: x.definition_func)
            output += colorful.white("\nYou have {0} pending step implementation{1} affecting {2} step{3}:\n  {4}\n\nNote: this could be the reason for some failing subsequent steps".format(
                len(pending_step_implementations),
                "s" if len(pending_step_implementations) is not 1 else "",
                len(pending_steps),
                "s" if len(pending_steps) is not 1 else "",
                "\n  ".join(["-  '{0}' @ {1}".format(sr.get_pattern(s.definition_func), get_func_code(s.definition_func).co_filename) for s in pending_step_implementations])
            ))

        output += "\n"
        output += colorful.cyan("Run {0} finished within {1}:{2} minutes".format(marker, int(duration.total_seconds()) / 60, duration.total_seconds() % 60.0))

        write(output)
Esempio n. 7
0
def console_writer_before_each_step(step):
    """
        Writes the step to the console before it is run

        :param Step step: the step to write to the console
    """
    global __LAST_PRECONDITION__
    if not isinstance(step.parent.parent, Feature):
        return

    if world.config.write_steps_once:
        return

    output = ""
    if step.as_precondition and __LAST_PRECONDITION__ != step.as_precondition:
        output += colorful.white("      As precondition from {}: {}\n".format(os.path.basename(step.as_precondition.path), step.as_precondition.sentence))
    elif not step.as_precondition and __LAST_PRECONDITION__:
        output += colorful.white("      From scenario\n")

    __LAST_PRECONDITION__ = step.as_precondition
    output += "\r        {}{}".format(get_id_sentence_prefix(step, colorful.bold_brown), colorful.bold_brown(step.sentence))

    if step.text:
        id_padding = get_id_padding(len(step.parent.steps))
        output += colorful.bold_white('\n            {}"""'.format(id_padding))
        output += colorful.cyan("".join(["\n                {}{}".format(id_padding, l) for l in step.raw_text]))
        output += colorful.bold_white('\n            {}"""'.format(id_padding))

    write(output)
Esempio n. 8
0
def test_step_matches_configs(match_config_files,
                              basedir,
                              cover_min_percentage=None,
                              cover_show_missing=False):
    """
    Test if the given match config files matches the actual
    matched step implementations.
    """
    if cover_min_percentage is not None and float(cover_min_percentage) > 100:
        sys.stderr.write(
            colorful.magenta(
                'You are a little cocky to think you can reach a minimum coverage of {0:.2f}%\n'
                .format(float(cover_min_percentage))))
        return 3

    # load user's custom python files
    load_modules(basedir)
    steps = StepRegistry().steps

    if not steps:
        sys.stderr.write(
            colorful.magenta(
                'No step implementations found in {0}, thus doesn\'t make sense to continue'
                .format(basedir)))
        return 4

    failed = 0
    passed = 0
    covered_steps = set()

    for match_config_file in match_config_files:
        # load the given match config file
        with codecs.open(match_config_file, "r", "utf-8") as f:
            match_config = yaml.safe_load(f)

        if not match_config:
            print(
                colorful.magenta(
                    'No sentences found in {0} to test against'.format(
                        match_config_file)))
            return 5
        else:
            print(
                colorful.brown('Testing sentences from {0}:'.format(
                    colorful.bold_brown(match_config_file))))
            failed_sentences, passed_senteces = test_step_matches(
                match_config, steps)
            failed += failed_sentences
            passed += passed_senteces

            covered_steps = covered_steps.union(x['should_match']
                                                for x in match_config)

        # newline
        sys.stdout.write('\n')

    report = colorful.bold_white('{0} sentences ('.format(failed + passed))
    if passed > 0:
        report += colorful.bold_green('{0} passed'.format(passed))

    if passed > 0 and failed > 0:
        report += colorful.bold_white(', ')

    if failed > 0:
        report += colorful.bold_red('{0} failed'.format(failed))
    report += colorful.bold_white(')')
    print(report)

    step_coverage = 100.0 / len(steps) * len(covered_steps)
    coverage_report = colorful.bold_white(
        'Covered {0} of {1} step implementations'.format(
            len(covered_steps), len(steps)))

    ret = 0 if failed == 0 else 1

    if cover_min_percentage:
        coverage_color = colorful.bold_green if step_coverage >= float(
            cover_min_percentage) else colorful.bold_red
        coverage_report += colorful.bold_white(' (coverage: ')
        coverage_report += coverage_color('{0:.2f}%'.format(step_coverage))
        if float(cover_min_percentage) > step_coverage:
            coverage_report += colorful.bold_white(
                ', expected a minimum of {0}'.format(
                    colorful.bold_green(cover_min_percentage + '%')))
            if failed == 0:
                ret = 2
            # if tests have passed and coverage is too low we fail with exit code 2
        coverage_report += colorful.bold_white(')')

    print(coverage_report)

    if cover_show_missing:
        missing_steps = get_missing_steps(steps, covered_steps)
        if missing_steps:
            missing_step_report = colorful.bold_brown('Missing steps:\n')
            for step in missing_steps:
                missing_step_report += '- {0} at '.format(
                    colorful.cyan(step[0]))
                missing_step_report += colorful.cyan(step[1]) + '\n'
            sys.stdout.write(missing_step_report)

    return ret