How to use fail_step method in grail

Best Python code snippet using grail_python

runner.py

Source:runner.py Github

copy

Full Screen

1from os import environ2import traceback3from typing import List, Dict, Optional4from dataclasses import dataclass5from timun.model import Feature, Scenario, ScenarioOutline, ScenarioType, Step6from timun.step import StepDescriptor, StepDict7from termcolor import colored8@dataclass9class ScenarioTestReport:10 feature: Optional[Feature]11 scenario: Scenario12 fail_step: Optional[Step] = None13 exception: Optional[Exception] = None14 step_desc: Optional[StepDescriptor] = None15 message: Optional[str] = None16 show_traceback = True17 def desc_line(self) -> str:18 res = f"{colored(self.scenario.type.name, 'blue')}\t: {self.scenario.text}"19 if self.feature:20 res += f"@{self.feature.filename}:{self.scenario.idx + 1} - "21 else:22 res += f"@GENERATED SCENARIOS - "23 if self.exception:24 res += f"{colored('FAILED', 'red')}:"25 else:26 res += colored("SUCCESS", "green")27 return res28 def __str__(self) -> str:29 res = self.desc_line()30 if self.exception:31 if self.fail_step:32 res += f"\n\tStep: {self.fail_step.text}:{self.fail_step.idx}"33 if self.step_desc:34 res += f" (desc {self.step_desc.function.__name__}"35 res += f"@{self.step_desc.filename}:{self.step_desc.function.__code__.co_firstlineno})"36 if self.message == None:37 self.message = "\n".join(self.exception.args)38 if self.message:39 lines = self.message.splitlines()40 if self.show_traceback and not isinstance(self.exception, AssertionError):41 for l in lines[:-3]:42 res += f"\n\t| {l}"43 for l in lines[-3:]:44 res += f"\n\t| {l}"45 return res46class TestRunner:47 def __init__(48 self, features: List[Feature], step_dict: Dict[str, StepDescriptor], environment49 ):50 self.features = features51 self.step_dict = step_dict52 self._indent = 053 self.environment = environment54 self.test_report: List[ScenarioTestReport] = []55 def run(self):56 self.test_report = []57 for feature in self.features:58 self.run_feature(feature)59 print("==== Failed Scenarios ====")60 for report in self.test_report:61 if report.exception != None:62 print(report)63 def run_feature(self, feature: Feature):64 print(f"==== Feature: {feature.text} @ {feature.filename}:{feature.idx+1}")65 for scenario in feature.scenarios:66 if isinstance(scenario, ScenarioOutline):67 raise Exception(68 f"Unexpanded scenario outline ({feature.filename}:{scenario.idx+1})"69 )70 report = run_scenario(self.step_dict, feature, scenario, self.environment)71 print(report.desc_line())72 self.test_report.append(report)73def run_scenario(74 step_dict: StepDict, feature: Optional[Feature], scenario: Scenario, environment75) -> ScenarioTestReport:76 context = {"failed": False, "scenario": scenario}77 report = ScenarioTestReport(feature, scenario)78 before_scenario_hook = getattr(environment, "before_scenario", None)79 if before_scenario_hook:80 before_scenario_hook(context)81 if feature and feature.background:82 for step in feature.background.steps:83 try:84 run_step(step_dict, step, context)85 except Exception as e:86 return create_report(e, feature, scenario, step, step_dict)87 scenario_type = scenario.type88 if scenario_type == ScenarioType.FAIL_SCENARIO:89 try:90 for step in scenario.steps:91 run_step(step_dict, step, context)92 report.exception = Exception(93 "All test in this scenario passes, expected failure"94 )95 except Exception as e:96 pass97 else:98 # NORMAL SCENARIO CASE99 for step in scenario.steps:100 try:101 run_step(step_dict, step, context)102 except Exception as e:103 return create_report(e, feature, scenario, step, step_dict)104 return report105def create_report(exception, feature, scenario, step, step_dict):106 report = ScenarioTestReport(feature, scenario)107 report.fail_step = step108 report.exception = exception109 try:110 report.step_desc, _ = find_matching_step(step_dict, step)111 except:112 report.step_desc = None113 report.show_traceback = False114 report.message = traceback.format_exc().strip()115 return report116def run_step(step_dict: StepDict, step: Step, context):117 step_definition, match = find_matching_step(step_dict, step)118 step_definition.function(context, **match.named)119def find_matching_step(step_dict: StepDict, step: Step):120 matching_step: List[StepDescriptor] = []121 # find matching step descriptor122 for desc in step_dict.values():123 match_pattern = desc.pattern.parse(step.text)124 match_type = desc.type == step.type125 if match_pattern and match_type:126 matching_step.append(desc)127 if matching_step == []:128 raise Exception(129 f"Can't find matching step definition for '{step.type.name} {step.text}'"130 )131 # TODO: Raise exception when multiple matching steps found...

Full Screen

Full Screen

log_utils.py

Source:log_utils.py Github

copy

Full Screen

1import tensorflow as tf2tf.compat.v1.enable_v2_behavior()3from tf_agents.trajectories import trajectory4def eval_fn(before_fail_step, fail_step, safe_step, after_safe_step, get_action):5 time_step, _, _ = trajectory.to_transition(before_fail_step, fail_step)6 fail_pol_ac = get_action(time_step)7 fail_ac = fail_step.action8 time_step, _, _ = trajectory.to_transition(safe_step, after_safe_step)9 init_pol_ac = get_action(time_step)10 init_ac = safe_step.action11 fail_pol_sc_input = (fail_step.observation, fail_pol_ac)12 fail_sc_input = (fail_step.observation, fail_ac)13 init_pol_sc_input = (safe_step.observation, init_pol_ac)14 init_sc_input = (safe_step.observation, init_ac)15 def eval_sc(safety_critic, step):16 p_fail_logits, _ = safety_critic(fail_pol_sc_input, fail_step.step_type,17 training=True)18 p_fail_onpol = tf.nn.sigmoid(p_fail_logits)19 p_fail_logits, _ = safety_critic(fail_sc_input, fail_step.step_type,20 training=True)21 p_fail = tf.nn.sigmoid(p_fail_logits)22 init_p_fail_logits, _ = safety_critic(init_pol_sc_input,23 safe_step.step_type, training=True)24 init_p_fail_onpol = tf.nn.sigmoid(init_p_fail_logits)25 init_p_fail_logits, _ = safety_critic(init_sc_input, safe_step.step_type,26 training=True)27 init_p_fail = tf.nn.sigmoid(init_p_fail_logits)28 tf.compat.v2.summary.histogram('p_fail_term_onpol', data=p_fail_onpol,29 step=step)30 tf.compat.v2.summary.histogram('p_fail_term', data=p_fail, step=step)31 tf.compat.v2.summary.histogram('p_fail_init_onpol',32 data=init_p_fail_onpol, step=step)33 tf.compat.v2.summary.histogram('p_fail_init', data=init_p_fail, step=step)...

Full Screen

Full Screen

pr_graph.py

Source:pr_graph.py Github

copy

Full Screen

1from IPython import embed as shell2from core.parsing.parse_solvers_config import ParseSolversConfig3from core.parsing.parse_domain_config import ParseDomainConfig4from core.parsing.parse_problem_config import ParseProblemConfig5from Queue import PriorityQueue6from prg_search_node import HLSearchNode, LLSearchNode7"""8Many methods called in p_mod_abs have detailed documentation.9"""10def p_mod_abs(domain_config, problem_config, solvers_config, max_iter=100):11 hl_solver, ll_solver = ParseSolversConfig.parse(solvers_config, domain_config)12 domain = ParseDomainConfig.parse(domain_config)13 problem = ParseProblemConfig.parse(problem_config, domain)14 if problem.goal_test():15 return False, "Goal is already satisfied. No planning done."16 n0 = HLSearchNode(hl_solver.translate_problem(problem), domain, problem)17 Q = PriorityQueue()18 Q.put((0, n0))19 for _ in range(max_iter):20 n = Q.get()[1]21 if n.is_hl_node():22 c_plan = n.plan(hl_solver)23 c = LLSearchNode(c_plan)24 Q.put((-n.heuristic(), n))25 Q.put((-c.heuristic(), c))26 elif n.is_ll_node():27 n.plan(ll_solver)28 if n.solved():29 return n.curr_plan, None30 Q.put((-n.heuristic(), n))31 if n.gen_child():32 fail_step, fail_pred = n.get_failed_pred()33 n_problem = n.get_problem(fail_step, fail_pred)34 c = HLSearchNode(hl_solver.translate_problem(n_problem), domain, n_problem, prefix=n.curr_plan.prefix(fail_step))35 Q.put((-c.heuristic(), c))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run grail automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful