Beispiel #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
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