Best Python code snippet using lemoncheesecake
runner.py
Source:runner.py
...51 teardown_funcs.append(teardown_func)52 else:53 teardown_funcs.append(teardown_func)54 return teardown_funcs55 def run_teardown_funcs(self, teardown_funcs):56 for teardown_func in teardown_funcs:57 if teardown_func:58 try:59 teardown_func()60 except Exception as e:61 self.handle_exception(e)62 def enable_task_abort(self):63 super(RunContext, self).enable_task_abort()64 self.session.aborted = True65 def is_task_to_be_skipped(self, task):66 # check for error in base implementation67 skip_reason = super(RunContext, self).is_task_to_be_skipped(task)68 if skip_reason:69 return skip_reason70 # check for error in event handling71 exception, _ = self.session.event_manager.get_pending_failure()72 if exception is not None:73 return str(exception)74 # check for test session abort75 if self._aborted_session:76 return "tests have been aborted"77 # check for suite abort78 if isinstance(task, TestTask):79 if task.test.parent_suite in self._aborted_suites:80 return "the tests of this test suite have been aborted"81 # check for --stop-on-failure82 if self.stop_on_failure and not self.session.is_successful():83 return "tests have been aborted on --stop-on-failure"84 return None85class TestTask(BaseTask):86 def __init__(self, test, suite_scheduled_fixtures, dependency=None):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_task321 self._dependencies = dependencies322 def get_on_completion_dependencies(self):323 return self._dependencies324 def run(self, context):325 if any(self.suite_setup_task.teardown_funcs):326 # before actual teardown327 context.session.start_suite_teardown(self.suite)328 context.session.set_step("Teardown suite")329 # actual teardown330 context.run_teardown_funcs(self.suite_setup_task.teardown_funcs)331 # after actual teardown332 context.session.end_suite_teardown(self.suite)333 def skip(self, context, _):334 self.run(context)335 def __str__(self):336 return "<%s %s>" % (self.__class__.__name__, self.suite.path)337def build_suite_teardown_task(suite, suite_setup_task, dependencies):338 return SuiteTeardownTask(suite, suite_setup_task, dependencies) if suite_setup_task else None339class TestSessionSetupTask(BaseTask):340 def __init__(self, scheduled_fixtures):341 BaseTask.__init__(self)342 self.scheduled_fixtures = scheduled_fixtures343 self.teardown_funcs = []344 def run(self, context):345 setup_teardown_funcs = self.scheduled_fixtures.get_setup_teardown_pairs()346 if any(setup for setup, _ in setup_teardown_funcs):347 # before actual setup348 context.session.start_test_session_setup()349 context.session.set_step("Setup test session")350 # actual setup351 self.teardown_funcs = context.run_setup_funcs(setup_teardown_funcs, ReportLocation.in_test_session_setup())352 # after actual setup353 context.session.end_test_session_setup()354 if not context.session.is_successful(ReportLocation.in_test_session_setup()):355 raise TaskFailure("test session setup failed")356 else:357 self.teardown_funcs = [teardown for _, teardown in setup_teardown_funcs if teardown]358def build_test_session_setup_task(scheduled_fixtures):359 return TestSessionSetupTask(scheduled_fixtures) if not scheduled_fixtures.is_empty() else None360class TestSessionTeardownTask(BaseTask):361 def __init__(self, test_session_setup_task, dependencies):362 BaseTask.__init__(self)363 self.test_session_setup_task = test_session_setup_task364 self._dependencies = dependencies365 def get_on_completion_dependencies(self):366 return self._dependencies367 def run(self, context):368 if any(self.test_session_setup_task.teardown_funcs):369 # before actual teardown370 context.session.start_test_session_teardown()371 context.session.set_step("Teardown test session")372 # actual teardown373 context.run_teardown_funcs(self.test_session_setup_task.teardown_funcs)374 # after actual teardown375 context.session.end_test_session_teardown()376 def skip(self, context, _):377 self.run(context)378def build_test_session_teardown_task(test_session_setup_task, dependencies):379 return TestSessionTeardownTask(test_session_setup_task, dependencies) if test_session_setup_task else None380def lookup_test_task(tasks, test_path):381 try:382 return next(task for task in tasks if isinstance(task, TestTask) and task.test.path == test_path)383 except StopIteration:384 raise LookupError("Cannot find test '%s' in tasks" % test_path)385def build_tasks(suites, fixture_registry, session_scheduled_fixtures, force_disabled):386 ###387 # Build test session setup task...
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!!