Best Python code snippet using lemoncheesecake
runner.py
Source:runner.py
...87 BaseTask.__init__(self)88 self.test = test89 self.suite_scheduled_fixtures = suite_scheduled_fixtures90 self.dependencies = [dependency] if dependency else []91 def get_on_success_dependencies(self):92 return self.dependencies93 def _is_test_disabled(self, context):94 return self.test.is_disabled() and not context.force_disabled95 def _handle_disabled_test(self, context):96 disabled = self.test.is_disabled()97 disabled_reason = disabled if isinstance(disabled, six.string_types) else None98 context.session.disable_test(self.test, disabled_reason)99 def skip(self, context, reason=None):100 if self._is_test_disabled(context):101 self._handle_disabled_test(context)102 else:103 context.session.skip_test(self.test, "Test skipped because %s" % reason if reason else None)104 @staticmethod105 def _prepare_test_args(test, scheduled_fixtures):106 args = {}107 for arg_name in test.get_arguments():108 if arg_name in test.parameters:109 args[arg_name] = test.parameters[arg_name]110 else:111 args[arg_name] = scheduled_fixtures.get_fixture_result(arg_name)112 return args113 def run(self, context):114 ###115 # Checker whether the test must be executed or not116 ###117 if self._is_test_disabled(context):118 self._handle_disabled_test(context)119 return120 ###121 # Begin test122 ###123 context.session.start_test(self.test)124 ###125 # Setup test (setup and fixtures)126 ###127 suite = self.test.parent_suite128 if suite.has_hook("setup_test"):129 def setup_test_wrapper():130 suite.get_hook("setup_test")(self.test)131 else:132 setup_test_wrapper = None133 if suite.has_hook("teardown_test"):134 def teardown_test_wrapper():135 status_so_far = "passed" if context.session.is_successful(ReportLocation.in_test(self.test)) else "failed"136 suite.get_hook("teardown_test")(self.test, status_so_far)137 else:138 teardown_test_wrapper = None139 setup_teardown_funcs = list()140 setup_teardown_funcs.append((setup_test_wrapper, teardown_test_wrapper))141 scheduled_fixtures = context.fixture_registry.get_fixtures_scheduled_for_test(142 self.test, self.suite_scheduled_fixtures143 )144 setup_teardown_funcs.extend(scheduled_fixtures.get_setup_teardown_pairs())145 context.session.set_step("Setup test")146 if any(setup for setup, _ in setup_teardown_funcs):147 teardown_funcs = context.run_setup_funcs(setup_teardown_funcs, ReportLocation.in_test(self.test))148 else:149 teardown_funcs = [teardown for _, teardown in setup_teardown_funcs if teardown]150 ###151 # Run test:152 ###153 if context.session.is_successful(ReportLocation.in_test(self.test)):154 test_args = self._prepare_test_args(self.test, scheduled_fixtures)155 context.session.set_step(self.test.description)156 try:157 self.test.callback(**test_args)158 except Exception as e:159 context.handle_exception(e, suite)160 ###161 # Teardown162 ###163 if any(teardown_funcs):164 context.session.set_step("Teardown test")165 context.run_teardown_funcs(teardown_funcs)166 context.session.end_test(self.test)167 if not context.session.is_successful(ReportLocation.in_test(self.test)):168 raise TaskFailure("test '%s' failed" % self.test.path)169 def __str__(self):170 return "<%s %s>" % (self.__class__.__name__, self.test.path)171def build_test_task(test, suite_scheduled_fixtures, dependency):172 return TestTask(test, suite_scheduled_fixtures, dependency)173def build_suite_tasks(174 suite, fixture_registry, session_scheduled_fixtures, test_session_setup_task,175 parent_suite_beginning_task=None, force_disabled=False):176 ###177 # Build suite beginning task178 ###179 suite_beginning_task = build_suite_beginning_task(180 suite, list((filter(bool, (test_session_setup_task, parent_suite_beginning_task))))181 )182 ###183 # Build suite setup task (if any)184 ###185 suite_scheduled_fixtures = fixture_registry.get_fixtures_scheduled_for_suite(186 suite, session_scheduled_fixtures, force_disabled187 )188 suite_setup_task = build_suite_initialization_task(189 suite, suite_scheduled_fixtures, [suite_beginning_task], force_disabled190 )191 ###192 # Build test tasks193 ###194 test_dependency = suite_setup_task if suite_setup_task else suite_beginning_task195 test_tasks = [196 build_test_task(test, suite_scheduled_fixtures, test_dependency)197 for test in suite.get_tests()198 ]199 ###200 # Build suite teardown task (if any)201 ###202 suite_teardown_task = build_suite_teardown_task(suite, suite_setup_task, test_tasks)203 ###204 # Build sub suite tasks205 ###206 sub_suite_tasks = []207 for sub_suite in suite.get_suites():208 sub_suite_tasks.extend(209 build_suite_tasks(210 sub_suite, fixture_registry, session_scheduled_fixtures, test_session_setup_task, suite_beginning_task,211 force_disabled212 )213 )214 ###215 # Build suite ending task216 ###217 suite_ending_dependencies = []218 suite_ending_dependencies.extend(test_tasks)219 if suite_teardown_task:220 suite_ending_dependencies.append(suite_teardown_task)221 suite_ending_dependencies.extend(222 task for task in sub_suite_tasks if isinstance(task, SuiteEndingTask) and task.suite in suite.get_suites()223 )224 suite_ending_task = build_suite_ending_task(suite, suite_ending_dependencies)225 ###226 # Return tasks != None227 ###228 task_iter = itertools.chain(229 (suite_beginning_task,),230 (suite_setup_task,), test_tasks, (suite_teardown_task,), sub_suite_tasks,231 (suite_ending_task,)232 )233 return list(filter(bool, task_iter))234class SuiteBeginningTask(BaseTask):235 def __init__(self, suite, dependencies):236 BaseTask.__init__(self)237 self.suite = suite238 self._dependencies = dependencies239 def get_on_success_dependencies(self):240 return self._dependencies241 def run(self, context):242 context.session.start_suite(self.suite)243 def skip(self, context, _):244 self.run(context)245 def __str__(self):246 return "<%s %s>" % (self.__class__.__name__, self.suite.path)247def build_suite_beginning_task(suite, dependencies):248 return SuiteBeginningTask(suite, dependencies)249class SuiteInitializationTask(BaseTask):250 def __init__(self, suite, setup_teardown_funcs, dependencies):251 BaseTask.__init__(self)252 self.suite = suite253 self.setup_teardown_funcs = setup_teardown_funcs254 self._dependencies = dependencies255 self.teardown_funcs = []256 def get_on_success_dependencies(self):257 return self._dependencies258 def run(self, context):259 if any(setup for setup, _ in self.setup_teardown_funcs):260 # before actual initialization261 context.session.start_suite_setup(self.suite)262 context.session.set_step("Setup suite")263 # actual initialization264 self.teardown_funcs = context.run_setup_funcs(265 self.setup_teardown_funcs, ReportLocation.in_suite_setup(self.suite)266 )267 # after actual initialization268 context.session.end_suite_setup(self.suite)269 if not context.session.is_successful(ReportLocation.in_suite_setup(self.suite)):270 raise TaskFailure("suite '%s' setup failed" % self.suite.path)271 else:272 self.teardown_funcs = [teardown for _, teardown in self.setup_teardown_funcs if teardown]273 def __str__(self):274 return "<%s %s>" % (self.__class__.__name__, self.suite.path)275def wrap_setup_suite(suite, scheduled_fixtures):276 setup_suite = suite.get_hook("setup_suite")277 if setup_suite is None:278 return None279 fixtures_names = suite.get_hook_params("setup_suite")280 def wrapper():281 fixtures = scheduled_fixtures.get_fixture_results(fixtures_names)282 setup_suite(**fixtures)283 return wrapper284def build_suite_initialization_task(suite, scheduled_fixtures, dependencies, force_disabled):285 if not suite.has_enabled_tests() and not force_disabled:286 return None287 setup_teardown_funcs = []288 if not scheduled_fixtures.is_empty():289 setup_teardown_funcs.extend(scheduled_fixtures.get_setup_teardown_pairs())290 if suite.get_injected_fixture_names():291 setup_teardown_funcs.append((292 lambda: suite.inject_fixtures(scheduled_fixtures.get_fixture_results(suite.get_injected_fixture_names())),293 None294 ))295 if suite.has_hook("setup_suite") or suite.has_hook("teardown_suite"):296 setup_teardown_funcs.append([297 wrap_setup_suite(suite, scheduled_fixtures),298 suite.get_hook("teardown_suite")299 ])300 return SuiteInitializationTask(suite, setup_teardown_funcs, dependencies) if setup_teardown_funcs else None301class SuiteEndingTask(BaseTask):302 def __init__(self, suite, dependencies):303 BaseTask.__init__(self)304 self.suite = suite305 self._dependencies = dependencies306 def get_on_success_dependencies(self):307 return self._dependencies308 def run(self, context):309 context.session.end_suite(self.suite)310 def skip(self, context, _):311 self.run(context)312 def __str__(self):313 return "<%s %s>" % (self.__class__.__name__, self.suite.path)314def build_suite_ending_task(suite, dependencies):315 return SuiteEndingTask(suite, dependencies)316class SuiteTeardownTask(BaseTask):317 def __init__(self, suite, suite_setup_task, dependencies):318 BaseTask.__init__(self)319 self.suite = suite320 self.suite_setup_task = suite_setup_task...
test_task.py
Source:test_task.py
...12 self.on_success_dependencies = on_success_dependencies or []13 self.on_completion_dependencies = on_completion_dependencies or []14 self.skipped = False15 self.exception_within_skip = None16 def get_on_success_dependencies(self):17 return self.on_success_dependencies18 def get_on_completion_dependencies(self):19 return self.on_completion_dependencies20 def skip(self, context, reason):21 self.skipped = True22 if self.exception_within_skip:23 raise self.exception_within_skip24 def __str__(self):25 return "<task %s>" % self.name26 def __repr__(self):27 return str(self)28class ExceptionTask(BaseTestTask):29 def __init__(self, name, exception, on_success_dependencies=None, on_completion_dependencies=None):30 BaseTestTask.__init__(self, name, on_success_dependencies, on_completion_dependencies)...
task.py
Source:task.py
...20class BaseTask(object):21 def __init__(self):22 self.result = None23 def get_all_dependencies(self):24 return self.get_on_completion_dependencies() + self.get_on_success_dependencies()25 def get_on_success_dependencies(self):26 return []27 def get_on_completion_dependencies(self):28 return []29 def run(self, context):30 pass31 def skip(self, context, reason):32 pass33class TaskContext(object):34 def __init__(self):35 self._tasks_aborted = False36 def enable_task_abort(self):37 self._tasks_aborted = True38 def is_task_to_be_skipped(self, task):39 if self._tasks_aborted:40 return "tests have been manually stopped"41 else:42 return None43def pop_runnable_tasks(remaining_tasks, completed_tasks, nb_tasks):44 runnable_tasks = list(45 itertools.islice(46 filter(lambda t: set(t.get_all_dependencies()).issubset(completed_tasks), remaining_tasks),47 0, nb_tasks48 )49 )50 for task in runnable_tasks:51 remaining_tasks.remove(task)52 _debug("pop runnable task %s" % task)53 yield task54def run_task(task, context, completed_task_queue):55 _debug("run task %s" % task)56 try:57 task.run(context)58 except TaskFailure as excp:59 task.result = TaskResultFailure(str(excp))60 except Exception:61 task.result = TaskResultException(serialize_current_exception())62 else:63 task.result = TaskResultSuccess()64 completed_task_queue.put(task)65def handle_task(task, context, completed_task_queue):66 _debug("handle task %s" % task)67 # skip task on dependency failure if any68 for dep_task in task.get_on_success_dependencies():69 if not isinstance(dep_task.result, TaskResultSuccess):70 if isinstance(dep_task.result, (TaskResultFailure, TaskResultSkipped)):71 reason = dep_task.result.reason72 else:73 reason = None74 skip_task(task, context, completed_task_queue, reason)75 return76 # skip task on external trigger if any77 skip_reason = context.is_task_to_be_skipped(task)78 if skip_reason:79 skip_task(task, context, completed_task_queue, reason=skip_reason)80 return81 # run task when all conditions are met82 run_task(task, context, completed_task_queue)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!