Exemple #1
0
def run_setup_teardown_tags(ctx: Context, feature: Feature) -> None:
    """
    Finds setup and teardown tags on scenarios in feature files. If present it handles them separately than normal tags

    Args:
        ctx: The behave context
        feature: The behave feature
    """
    remaining_scenarios = []
    ctx.teardown_scenarios = []
    # Check each feature to see if we have setup or teardown tags. Else they are normal scenarios
    for scenario in feature.scenarios:
        # Pipe the feature and scenario into the context so we can use it downstream in child steps
        ctx.feature = feature
        ctx.scenario = scenario
        if "setup" in scenario.tags:
            LOGGER.info(
                "Setup scenario detected. Running it before the rest of the feature."
            )
            # Run the steps in the setup scenario as setup
            execute_scenario_by_steps(ctx, scenario)
        elif "teardown" in scenario.tags:
            LOGGER.info(
                "Teardown scenario detected. Saving it so we can run as cleanup after the rest of the feature."
            )
            ctx.teardown_scenarios.append(scenario)
        else:
            remaining_scenarios.append(scenario)
    feature.scenarios = remaining_scenarios
Exemple #2
0
def after_feature(ctx: Context, feature: Feature):
    """
    Executed once after each feature suite

    Args:
        ctx: The behave context object.
        feature: The behave feature object
    """
    # Run all the teardown scenarios that were identified in `before_feature`
    for scenario in ctx.teardown_scenarios:
        LOGGER.info(
            "Teardown scenario found in teardown list. Running it now that our feature is complete."
        )
        # Pipe the scenario into the context so we can use it downstream in child steps
        ctx.feature = feature
        ctx.scenario = scenario
        # Execute the steps of each scenario as teardown
        execute_scenario_by_steps(ctx, scenario)
def run_teardown_tags(ctx: Context, feature: Feature) -> None:
    """Run all the teardown scenarios that were identified in `before_feature`

    Args:
        ctx: The behave context
        feature: The behave feature object

    """
    for scenario in ctx.teardown_scenarios:
        LOGGER.debug(
            "Teardown scenario found in teardown list. Running it now that our feature is complete."
        )
        # Pipe the scenario into the context so we can use it downstream in child steps
        ctx.feature = feature
        ctx.scenario = scenario
        # Execute the steps of each scenario as teardown
        print(
            f"\n  {ansicolor.yellow('@teardown')}\n  Scenario: {scenario.name}"
        )  # noqa
        execute_scenario_by_steps(ctx, scenario)
def run_setup_tags(ctx: Context, feature: Feature) -> None:
    """Handles setup and teardown tags on scenarios in feature files.

     If @setup or @teardown is present it handles them separately than normal tags.
     These tags will be run before and after entire feature by inserting the steps into
     behave runner via the step executor attached to the context. This is the only way to
     do this so the setup and teardown "scenario" is not counted towards the test results.

     @setup tags are run now in this function.
     @teardown tags are placed on the context in the root layer to be run in the `after feature` hook

    Args:
        ctx: The behave context
        feature: The behave feature

    """
    remaining_scenarios = []
    ctx.teardown_scenarios = []
    # Check each feature to see if we have setup or teardown tags. Else they are normal scenarios
    for scenario in feature.scenarios:
        # Pipe the feature and scenario into the context so we can use it downstream in child steps
        ctx.feature = feature
        ctx.scenario = scenario
        if "setup" in scenario.tags:
            LOGGER.debug(
                "Setup scenario detected. Running it before the rest of the feature."
            )
            print(
                f"\n  {ansicolor.yellow('@setup')}\n  Scenario: {scenario.name}"
            )  # noqa
            # Run the steps in the setup scenario as setup
            execute_scenario_by_steps(ctx, scenario)
        elif "teardown" in scenario.tags:
            LOGGER.debug(
                "Teardown scenario detected. Saving it so we can run as cleanup after the rest of the feature."
            )
            ctx.teardown_scenarios.append(scenario)
        else:
            remaining_scenarios.append(scenario)
    feature.scenarios = remaining_scenarios