Best Python code snippet using lemoncheesecake
listener.py
Source:listener.py
...179 self.tests = dict()180 self.current_suite_id = 0181 self.current_test_id = 0182 self.report_path = ""183 def _get_suite_result(self, test_result, create=False):184 if test_result.index in self.suites:185 return self.suites.get(test_result.index)186 elif create:187 suite = SuiteResult()188 rid = uuid.uuid4().hex if test_result.index == "" else \189 test_result.index190 suite.index = rid191 return self.suites.setdefault(rid, suite)192 else:193 return self.suites.get(self.current_suite_id)194 def _get_test_result(self, test_result, create=False):195 if test_result.index in self.tests:196 return self.tests.get(test_result.index)197 elif create:198 test = CaseResult()199 rid = uuid.uuid4().hex if test_result.index == "" else \200 test_result.index201 test.index = rid202 return self.tests.setdefault(rid, test)203 else:204 return self.tests.get(self.current_test_id)205 def __started__(self, lifecycle, test_result):206 if lifecycle == LifeCycle.TestSuites:207 suites = self._get_suite_result(test_result=test_result,208 create=True)209 suites.suites_name = test_result.suites_name210 suites.test_num = test_result.test_num211 self.current_suite_id = suites.index212 elif lifecycle == LifeCycle.TestSuite:213 suite = self._get_suite_result(test_result=test_result,214 create=True)215 suite.suite_name = test_result.suite_name216 suite.test_num = test_result.test_num217 self.current_suite_id = suite.index218 elif lifecycle == LifeCycle.TestCase:219 test = self._get_test_result(test_result=test_result, create=True)220 test.test_name = test_result.test_name221 test.test_class = test_result.test_class222 self.current_test_id = test.index223 def __ended__(self, lifecycle, test_result=None, **kwargs):224 if lifecycle == LifeCycle.TestSuite:225 suite = self._get_suite_result(test_result=test_result,226 create=False)227 if not suite:228 return229 suite.run_time = test_result.run_time230 suite.code = test_result.code231 is_clear = kwargs.get("is_clear", False)232 suite.test_num = max(test_result.test_num, len(self.tests))233 # generate suite report234 if not kwargs.get("suite_report", False):235 if len(self.result) > 0 and self.result[-1][0].suite_name == \236 self.suites[suite.index].suite_name:237 self.result[-1][1].extend(list(self.tests.values()))238 self.result[-1][0].test_num = max(suite.test_num,239 len(self.result[-1][1]))240 else:241 self.result.append((self.suites[suite.index],242 list(self.tests.values())))243 else:244 result_dir = os.path.join(self.report_path, "result")245 os.makedirs(result_dir, exist_ok=True)246 self.result.append((self.suites[suite.index],247 list(self.tests.values())))248 results = [(suite, list(self.tests.values()))]249 suite_report = SuiteReporter(results, suite.suite_name,250 result_dir)251 suite_report.generate_data_report()252 if is_clear:253 self.tests.clear()254 elif lifecycle == LifeCycle.TestSuites:255 if not kwargs.get("suite_report", False):256 result_dir = os.path.join(self.report_path, "result")257 os.makedirs(result_dir, exist_ok=True)258 suites_name = kwargs.get("suites_name", "")259 product_params = kwargs.get("product_params", "")260 suite_report = SuiteReporter(self.result, suites_name,261 result_dir,262 product_params=product_params)263 suite_report.generate_data_report()264 elif lifecycle == LifeCycle.TestCase:265 test = self._get_test_result(test_result=test_result, create=False)266 test.run_time = test_result.run_time267 test.stacktrace = test_result.stacktrace268 test.code = test_result.code269 elif lifecycle == LifeCycle.TestTask:270 result_report = ResultReporter(report_path=self.report_path,271 task_info=test_result)272 result_report.generate_reports()273 def __skipped__(self, lifecycle, test_result):274 if lifecycle == LifeCycle.TestSuite:275 suite = self._get_suite_result(test_result=test_result,276 create=False)277 suite.code = ResultCode.SKIPPED.value278 elif lifecycle == LifeCycle.TestCase:279 test = self._get_test_result(test_result=test_result, create=False)280 test.code = ResultCode.SKIPPED.value281 def __failed__(self, lifecycle, test_result):282 if lifecycle == LifeCycle.TestSuite:283 suite = self._get_suite_result(test_result=test_result,284 create=False)285 suite.stacktrace = test_result.stacktrace286 suite.code = ResultCode.FAILED.value287 elif lifecycle == LifeCycle.TestCase:288 test = self._get_test_result(test_result=test_result, create=False)289 test.stacktrace = test_result.stacktrace290 test.code = ResultCode.FAILED.value291@Plugin(type=Plugin.LISTENER, id=ListenerType.upload)292class UploadListener(IListener):293 def __started__(self, lifecycle, test_result):294 pass295 @staticmethod296 def __ended__(lifecycle, test_result, **kwargs):297 del test_result, kwargs...
writer.py
Source:writer.py
...9 self.report = report10 self.active_steps = {}11 def _get_test_result(self, test):12 return self.report.get_test(test)13 def _get_suite_result(self, suite):14 return self.report.get_suite(suite)15 def _add_step_log(self, log, event):16 result = self.report.get(event.location)17 assert result, "Cannot find location %s in the report" % event.location18 step = self._lookup_step(event)19 assert step, "Cannot find active step for %s" % event.location20 assert not step.end_time, "Cannot update step '%s', it has already been ended" % step.description21 step.add_log(log)22 @staticmethod23 def _initialize_result(start_time):24 result = Result()25 result.start_time = start_time26 return result27 @staticmethod28 def _initialize_test_result(test, start_time):29 result = TestResult(test.name, test.description)30 result.tags.extend(test.tags)31 result.properties.update(test.properties)32 result.links.extend(test.links)33 result.rank = test.rank34 result.start_time = start_time35 return result36 @staticmethod37 def _finalize_result(result, end_time):38 result.end_time = end_time39 result.status = "passed" if result.is_successful() else "failed"40 def _lookup_step(self, event):41 try:42 return self.active_steps[event.thread_id]43 except KeyError:44 return None45 def on_test_session_start(self, event):46 self.report.start_time = event.time47 def on_test_session_end(self, event):48 self.report.end_time = event.time49 def on_test_session_setup_start(self, event):50 self.report.test_session_setup = self._initialize_result(event.time)51 def on_test_session_setup_end(self, event):52 self._finalize_result(self.report.test_session_setup, event.time)53 def on_test_session_teardown_start(self, event):54 self.report.test_session_teardown = self._initialize_result(event.time)55 def on_test_session_teardown_end(self, event):56 self._finalize_result(self.report.test_session_teardown, event.time)57 def on_suite_start(self, event):58 suite = event.suite59 suite_result = SuiteResult(suite.name, suite.description)60 suite_result.start_time = event.time61 suite_result.tags.extend(suite.tags)62 suite_result.properties.update(suite.properties)63 suite_result.links.extend(suite.links)64 suite_result.rank = suite.rank65 if suite.parent_suite:66 parent_suite_result = self._get_suite_result(suite.parent_suite)67 parent_suite_result.add_suite(suite_result)68 else:69 self.report.add_suite(suite_result)70 def on_suite_end(self, event):71 suite_result = self._get_suite_result(event.suite)72 suite_result.end_time = event.time73 def on_suite_setup_start(self, event):74 suite_result = self._get_suite_result(event.suite)75 suite_result.suite_setup = self._initialize_result(event.time)76 def on_suite_setup_end(self, event):77 suite_result = self._get_suite_result(event.suite)78 self._finalize_result(suite_result.suite_setup, event.time)79 def on_suite_teardown_start(self, event):80 suite_result = self._get_suite_result(event.suite)81 suite_result.suite_teardown = self._initialize_result(event.time)82 def on_suite_teardown_end(self, event):83 suite_result = self._get_suite_result(event.suite)84 self._finalize_result(suite_result.suite_teardown, event.time)85 def on_test_start(self, event):86 test_result = self._initialize_test_result(event.test, event.time)87 suite_result = self._get_suite_result(event.test.parent_suite)88 suite_result.add_test(test_result)89 def on_test_end(self, event):90 test_result = self._get_test_result(event.test)91 self._finalize_result(test_result, event.time)92 def _bypass_test(self, test, status, status_details, time):93 test_result = self._initialize_test_result(test, time)94 test_result.end_time = time95 test_result.status = status96 test_result.status_details = status_details97 suite_result = self._get_suite_result(test.parent_suite)98 suite_result.add_test(test_result)99 def on_test_skipped(self, event):100 self._bypass_test(event.test, "skipped", event.skipped_reason, event.time)101 def on_test_disabled(self, event):102 self._bypass_test(event.test, "disabled", event.disabled_reason, event.time)103 def on_step_start(self, event):104 result = self.report.get(event.location)105 step = Step(event.step_description)106 step.start_time = event.time107 result.add_step(step)108 self.active_steps[event.thread_id] = step109 def on_step_end(self, event):110 step = self._lookup_step(event)111 step.end_time = event.time...
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!!