def patch_scenario_with_autoretry(scenario, max_attempts=3):
    def scenario_run_with_retries(scenario_run, *args, **kwargs):
        for attempt in range(1, max_attempts+1):
            if not scenario_run(*args, **kwargs):
                if attempt > 1:
                    print(u"AUTO-RETRY SCENARIO PASSED (after {0} attempts)".format(attempt))
                return False    # -- NOT-FAILED = PASSED
            # -- SCENARIO FAILED:
            if attempt < max_attempts:
                print(u"AUTO-RETRY SCENARIO (attempt {0})".format(attempt))
        print(u"AUTO-RETRY SCENARIO FAILED (after {0} attempts)".format(max_attempts))
        return True

    if isinstance(scenario, ScenarioOutline):
        scenario_outline = scenario
        for scenario in scenario_outline.scenarios:
            scenario_run = scenario.run
            scenario.run = functools.partial(scenario_run_with_retries, scenario_run)
    else:
        scenario_run = scenario.run
        scenario.run = functools.partial(scenario_run_with_retries, scenario_run)
Monkey-patches :func:`~behave.model.Scenario.run()` to auto-retry a 
dataset/ETHPy150Open behave/behave/behave/contrib/scenario_autoretry.py/patch_scenario_with_autoretry