Best Python code snippet using lemoncheesecake
xml.py
Source:xml.py
...40 attr_name, attr_value = args[i], args[i+1]41 node.attrib[attr_name] = attr_value42 i += 243 return node44def make_xml_child(parent, name, *args):45 child = make_xml_node(name, *args)46 parent.append(child)47 return child48def _serialize_time(value):49 return format_time_as_iso8601(value)50def _serialize_bool(value):51 return "true" if value else "false"52def _serialize_steps(steps, xml_result):53 for step in steps:54 xml_step = make_xml_child(55 xml_result, "step",56 "description", step.description,57 "start-time", _serialize_time(step.start_time)58 )59 if step.end_time is not None:60 xml_step.attrib["end-time"] = _serialize_time(step.end_time)61 for log in step.get_logs():62 if isinstance(log, Log):63 xml_log = make_xml_child(64 xml_step, "log",65 "level", log.level,66 "time", _serialize_time(log.time)67 )68 xml_log.text = log.message69 elif isinstance(log, Attachment):70 xml_attachment = make_xml_child(71 xml_step, "attachment",72 "description", log.description,73 "as-image", _serialize_bool(log.as_image),74 "time", _serialize_time(log.time)75 )76 xml_attachment.text = log.filename77 elif isinstance(log, Url):78 xml_url = make_xml_child(79 xml_step, "url",80 "description", log.description,81 "time", _serialize_time(log.time)82 )83 xml_url.text = log.url84 else: # TestCheck85 xml_check = make_xml_child(86 xml_step, "check",87 "description", log.description,88 "is-successful", _serialize_bool(log.is_successful),89 "time", _serialize_time(log.time)90 )91 xml_check.text = log.details92def _serialize_result(result, xml_result):93 if result.status:94 xml_result.attrib["status"] = result.status95 if result.status_details:96 xml_result.attrib["status-details"] = result.status_details97 xml_result.attrib["start-time"] = _serialize_time(result.start_time)98 if result.end_time is not None:99 xml_result.attrib["end-time"] = _serialize_time(result.end_time)100 _serialize_steps(result.get_steps(), xml_result)101def _serialize_node_metadata(obj, xml_node):102 xml_node.attrib["name"] = obj.name103 xml_node.attrib["description"] = obj.description104 for tag in obj.tags:105 xml_tag = make_xml_child(xml_node, "tag")106 xml_tag.text = tag107 for name, value in obj.properties.items():108 xml_property = make_xml_child(xml_node, "property", "name", name)109 xml_property.text = value110 for link in obj.links:111 xml_link = make_xml_child(xml_node, "link")112 if link[1]:113 xml_link.attrib["name"] = link[1]114 xml_link.text = link[0]115def _serialize_test_result(test):116 xml_test = make_xml_node("test")117 _serialize_node_metadata(test, xml_test)118 _serialize_result(test, xml_test)119 return xml_test120def _serialize_suite_result(suite):121 xml_suite = make_xml_node("suite")122 _serialize_node_metadata(suite, xml_suite)123 xml_suite.attrib["start-time"] = _serialize_time(suite.start_time)124 if suite.end_time is not None:125 xml_suite.attrib["end-time"] = _serialize_time(suite.end_time)126 # before suite127 if suite.suite_setup:128 _serialize_result(suite.suite_setup, make_xml_child(xml_suite, "suite-setup"))129 # tests130 xml_suite.extend(map(_serialize_test_result, suite.get_tests()))131 # sub suites132 xml_suite.extend(map(_serialize_suite_result, suite.get_suites()))133 # after suite134 if suite.suite_teardown:135 _serialize_result(suite.suite_teardown, make_xml_child(xml_suite, "suite-teardown"))136 return xml_suite137def serialize_report_as_xml_tree(report):138 xml_report = E("lemoncheesecake-report")139 xml_report.attrib["lemoncheesecake-version"] = lemoncheesecake.__version__140 xml_report.attrib["report-version"] = "1.1"141 xml_report.attrib["start-time"] = _serialize_time(report.start_time)142 if report.end_time is not None:143 xml_report.attrib["end-time"] = _serialize_time(report.end_time)144 xml_report.attrib["generation-time"] = _serialize_time(time.time())145 xml_report.attrib["nb-threads"] = str(report.nb_threads)146 xml_title = make_xml_child(xml_report, "title")147 xml_title.text = report.title148 for name, value in report.info:149 xml_info = make_xml_child(xml_report, "info", "name", name)150 xml_info.text = value151 if report.test_session_setup:152 _serialize_result(report.test_session_setup, make_xml_child(xml_report, "test-session-setup"))153 xml_report.extend(map(_serialize_suite_result, report.get_suites()))154 if report.test_session_teardown:155 _serialize_result(report.test_session_teardown, make_xml_child(xml_report, "test-session-teardown"))156 return xml_report157def serialize_report_as_string(report, indent_level=DEFAULT_INDENT_LEVEL):158 xml_report = serialize_report_as_xml_tree(report)159 indent_xml(xml_report, indent_level=indent_level)160 if six.PY3:161 return ET.tostring(xml_report, pretty_print=True, encoding="unicode")162 else:163 return ET.tostring(xml_report, pretty_print=True, xml_declaration=True, encoding="utf-8")164def save_report_into_file(report, filename, indent_level=DEFAULT_INDENT_LEVEL):165 content = serialize_report_as_string(report, indent_level)166 with open(filename, "w") as fh:167 fh.write(content)168def _unserialize_time(value):169 return parse_iso8601_time(value)...
junit.py
Source:junit.py
...27 "time", _serialization_duration(test.duration or 0),28 )29 30 if test.status == "skipped":31 make_xml_child(xml_test, "skipped")32 else:33 for step in test.get_steps():34 for log in step.get_logs():35 if isinstance(log, Check) and log.is_successful is False:36 make_xml_child(37 xml_test, "failure",38 "message", "failed check in step '%s', %s%s" % (39 step.description, log.description,40 (": " + log.details) if log.details else ""41 )42 )43 elif isinstance(log, Log) and log.level == Log.LEVEL_ERROR:44 make_xml_child(45 xml_test, "error",46 "message", "error log in step '%s': %s" % (47 step.description, log.message48 )49 )50 return xml_test51def _serialize_suite_result(suite):52 tests = suite.get_tests()53 xml_suite = make_xml_node(54 "testsuite",55 "name", suite.path,56 "tests", str(len(tests)),57 "failures", str(len(list(filter(lambda t: t.status == "failed", tests)))),58 "skipped", str(len(list(filter(lambda t: t.status == "skipped", tests)))),...
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!!