How to use get_setup_teardown_pairs method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

runner.py

Source:runner.py Github

copy

Full Screen

...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 task388 ###389 test_session_setup_task = build_test_session_setup_task(session_scheduled_fixtures)390 ###391 # Build suite tasks392 ###393 suite_tasks = []394 for suite in suites:395 suite_tasks.extend(396 build_suite_tasks(397 suite, fixture_registry, session_scheduled_fixtures, test_session_setup_task,398 force_disabled=force_disabled399 )400 )401 ###402 # Build test session teardown task403 ###404 if test_session_setup_task:405 test_session_teardown_dependencies = [406 task for task in suite_tasks if isinstance(task, SuiteEndingTask) and task.suite in suites407 ]408 test_session_teardown_task = build_test_session_teardown_task(409 test_session_setup_task, test_session_teardown_dependencies410 )411 else:412 test_session_teardown_task = None413 ###414 # Get all effective tasks (task != None)415 ###416 task_iter = itertools.chain((test_session_setup_task,), suite_tasks, (test_session_teardown_task,))417 tasks = list(filter(bool, task_iter))418 ###419 # Add extra dependencies in tasks for tests that depend on other tests420 ###421 for test in flatten_tests(suites):422 if not test.dependencies:423 continue424 test_task = lookup_test_task(tasks, test.path)425 for dep_test_path in test.dependencies:426 try:427 dep_test = lookup_test_task(tasks, dep_test_path)428 except LookupError:429 raise UserError(430 "Cannot find dependency test '%s' for '%s', "431 "either the test does not exist or is not going to be run" % (dep_test_path, test.path)432 )433 test_task.dependencies.append(dep_test)434 ###435 # Return tasks436 ###437 return tasks438def _run_suites(suites, fixture_registry, pre_run_scheduled_fixtures, session,439 force_disabled=False, stop_on_failure=False, nb_threads=1):440 # build tasks and run context441 session_scheduled_fixtures = fixture_registry.get_fixtures_scheduled_for_session(442 suites, pre_run_scheduled_fixtures, force_disabled443 )444 tasks = build_tasks(suites, fixture_registry, session_scheduled_fixtures, force_disabled)445 context = RunContext(session, fixture_registry, force_disabled, stop_on_failure)446 with session.event_manager.handle_events():447 session.start_test_session()448 run_tasks(tasks, context, nb_threads)449 session.end_test_session()450 exception, serialized_exception = session.event_manager.get_pending_failure()451 if exception:452 raise exception.__class__(serialized_exception)453def run_suites(suites, fixture_registry, session, force_disabled=False, stop_on_failure=False, nb_threads=1):454 fixture_teardowns = []455 # setup of 'pre_run' fixtures456 errors = []457 scheduled_fixtures = fixture_registry.get_fixtures_scheduled_for_pre_run(suites, force_disabled)458 initialize_fixture_cache(scheduled_fixtures)459 for setup, teardown in scheduled_fixtures.get_setup_teardown_pairs():460 try:461 setup()462 except UserError:463 raise464 except Exception:465 errors.append("Got the following exception when executing fixture (scope 'pre_run')%s" % (466 serialize_current_exception(show_stacktrace=True)467 ))468 break469 fixture_teardowns.append(teardown)470 if not errors:471 _run_suites(472 suites, fixture_registry, scheduled_fixtures, session,473 force_disabled=force_disabled, stop_on_failure=stop_on_failure, nb_threads=nb_threads...

Full Screen

Full Screen

fixture.py

Source:fixture.py Github

copy

Full Screen

...154 def _teardown_fixture(self, name):155 assert name in self._results, "Cannot teardown fixture '%s', it has not been previously executed" % name156 self._results[name].teardown()157 del self._results[name]158 def get_setup_teardown_pairs(self):159 return list(map(160 lambda name: (lambda: self._setup_fixture(name), lambda: self._teardown_fixture(name)),161 self._fixtures162 ))163 def get_fixture_result(self, name):164 if name in self._fixtures:165 assert name in self._results, "Cannot get fixture '%s' result, it has not been previously executed" % name166 return self._results[name].get()167 elif self._parent_scheduled_fixtures:168 return self._parent_scheduled_fixtures.get_fixture_result(name)169 else:170 raise LookupError("Cannot find fixture named '%s' in scheduled fixtures" % name)171 def get_fixture_results(self, names):172 return {name: self.get_fixture_result(name) for name in names}...

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 Lemoncheesecake 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