How to use save_report_into_file method in Lemoncheesecake

Best Python code snippet using lemoncheesecake

xml.py

Source:xml.py Github

copy

Full Screen

...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)170def _unserialize_bool(value):171 if value == "true":172 return True173 elif value == "false":174 return False175 else:176 raise ValueError("Invalid boolean representation: '%s'" % value)177def _unserialize_step(xml_step):178 step = Step(xml_step.attrib["description"])179 step.start_time = _unserialize_time(xml_step.attrib["start-time"])180 step.end_time = _unserialize_time(xml_step.attrib["end-time"]) if "end-time" in xml_step.attrib else None181 for xml_log in xml_step:182 if xml_log.tag == "log":183 step_log = Log(184 xml_log.attrib["level"], xml_log.text, _unserialize_time(xml_log.attrib["time"])185 )186 elif xml_log.tag == "attachment":187 step_log = Attachment(188 xml_log.attrib["description"], xml_log.text,189 _unserialize_bool(xml_log.attrib["as-image"]),190 _unserialize_time(xml_log.attrib["time"])191 )192 elif xml_log.tag == "url":193 step_log = Url(194 xml_log.attrib["description"], xml_log.text, _unserialize_time(xml_log.attrib["time"])195 )196 elif xml_log.tag == "check":197 step_log = Check(198 xml_log.attrib["description"], _unserialize_bool(xml_log.attrib["is-successful"]),199 xml_log.text, _unserialize_time(xml_log.attrib["time"])200 )201 else:202 raise ValueError("Unknown tag '%s' for step" % xml_log.tag)203 step.add_log(step_log)204 return step205def _unserialize_result(xml_result, result):206 result.status = xml_result.attrib.get("status", None)207 # status_details for non-test results has been introduced in report version 1.1:208 result.status_details = xml_result.attrib.get("status-details", None)209 result.start_time = _unserialize_time(xml_result.attrib["start-time"])210 result.end_time = _unserialize_time(xml_result.attrib["end-time"]) if "end-time" in xml_result.attrib else None211 for xml_step in xml_result.xpath("step"):212 result.add_step(_unserialize_step(xml_step))213def _unserialize_node_metadata(xml_node, node):214 node.tags = [n.text for n in xml_node.xpath("tag")]215 node.properties = {n.attrib["name"]: n.text for n in xml_node.xpath("property")}216 node.links = [(n.text, n.attrib.get("name", None)) for n in xml_node.xpath("link")]217def _unserialize_test_result(xml_result):218 test = TestResult(xml_result.attrib["name"], xml_result.attrib["description"])219 _unserialize_result(xml_result, test)220 _unserialize_node_metadata(xml_result, test)221 return test222def _unserialize_suite_result(xml_suite):223 suite = SuiteResult(xml_suite.attrib["name"], xml_suite.attrib["description"])224 suite.start_time = _unserialize_time(xml_suite.attrib["start-time"])225 suite.end_time = _unserialize_time(xml_suite.attrib["end-time"]) if "end-time" in xml_suite.attrib else None226 _unserialize_node_metadata(xml_suite, suite)227 xml_setup = xml_suite.xpath("suite-setup")228 xml_setup = xml_setup[0] if len(xml_setup) > 0 else None229 if xml_setup is not None:230 suite.suite_setup = Result()231 _unserialize_result(xml_setup, suite.suite_setup)232 for xml_test in xml_suite.xpath("test"):233 suite.add_test(_unserialize_test_result(xml_test))234 xml_teardown = xml_suite.xpath("suite-teardown")235 xml_teardown = xml_teardown[0] if len(xml_teardown) > 0 else None236 if xml_teardown is not None:237 suite.suite_teardown = Result()238 _unserialize_result(xml_teardown, suite.suite_teardown)239 for xml_suite in xml_suite.xpath("suite"):240 suite.add_suite(_unserialize_suite_result(xml_suite))241 return suite242def _unserialize_report(xml_report):243 report = Report()244 report.start_time = _unserialize_time(xml_report.attrib["start-time"])245 report.end_time = _unserialize_time(xml_report.attrib["end-time"]) if "end-time" in xml_report.attrib else None246 report.saving_time = _unserialize_time(xml_report.attrib["generation-time"]) if "generation-time" in xml_report.attrib else None247 report.nb_threads = int(xml_report.attrib["nb-threads"])248 report.title = xml_report.xpath("title")[0].text249 report.info = [(node.attrib["name"], node.text) for node in xml_report.xpath("info")]250 xml_setup = xml_report.xpath("test-session-setup")251 xml_setup = xml_setup[0] if len(xml_setup) else None252 if xml_setup is not None:253 report.test_session_setup = Result()254 _unserialize_result(xml_setup, report.test_session_setup)255 for xml_suite in xml_report.xpath("suite"):256 report.add_suite(_unserialize_suite_result(xml_suite))257 xml_teardown = xml_report.xpath("test-session-teardown")258 xml_teardown = xml_teardown[0] if len(xml_teardown) else None259 if xml_teardown is not None:260 report.test_session_teardown = Result()261 _unserialize_result(xml_teardown, report.test_session_teardown)262 return report263def load_report_from_file(filename):264 try:265 with open(filename, "r") as fh:266 xml = ET.parse(fh)267 except ET.LxmlError as e:268 raise ReportLoadingError(str(e))269 except IOError as e:270 raise e # re-raise as-is271 try:272 root = xml.getroot().xpath("/lemoncheesecake-report")[0]273 except IndexError:274 raise ReportLoadingError("Cannot find lemoncheesecake-report element in XML")275 report_version = float(root.attrib["report-version"])276 if report_version >= 2.0:277 raise ReportLoadingError("Incompatible report version: got %s while 1.x is supported" % report_version)278 return _unserialize_report(root)279class XmlBackend(FileReportBackend, ReportUnserializerMixin):280 def __init__(self):281 self.indent_level = DEFAULT_INDENT_LEVEL282 def get_name(self):283 return "xml"284 def is_available(self):285 return LXML_IS_AVAILABLE286 def get_report_filename(self):287 return "report.xml"288 def save_report(self, filename, report):289 save_report_into_file(report, filename, self.indent_level)290 def load_report(self, path):291 report = load_report_from_file(path)292 report.bind(self, path)...

Full Screen

Full Screen

json_.py

Source:json_.py Github

copy

Full Screen

...116 json_report["suites"] = list(map(_serialize_suite_result, report.get_suites()))117 if report.test_session_teardown:118 json_report["test_session_teardown"] = _serialize_result(report.test_session_teardown)119 return json_report120def save_report_into_file(report, filename, javascript_compatibility=True, pretty_formatting=False):121 json_report = serialize_report_into_json(report)122 with open(filename, "w") as fh:123 if javascript_compatibility:124 fh.write(JS_PREFIX)125 if pretty_formatting:126 fh.write(json.dumps(json_report, indent=4))127 else:128 fh.write(json.dumps(json_report))129def _unserialize_time(t):130 return parse_iso8601_time(t) if t is not None else None131def _unserialize_step(json_step):132 step = Step(json_step["description"])133 step.start_time = _unserialize_time(json_step["start_time"])134 step.end_time = _unserialize_time(json_step["end_time"])135 for json_log in json_step["entries"]:136 if json_log["type"] == "log":137 step_log = Log(138 json_log["level"], json_log["message"], _unserialize_time(json_log["time"])139 )140 elif json_log["type"] == "attachment":141 step_log = Attachment(142 json_log["description"], json_log["filename"], json_log["as_image"],143 _unserialize_time(json_log["time"])144 )145 elif json_log["type"] == "url":146 step_log = Url(147 json_log["description"], json_log["url"], _unserialize_time(json_log["time"])148 )149 elif json_log["type"] == "check":150 step_log = Check(151 json_log["description"], json_log["is_successful"], json_log["details"],152 _unserialize_time(json_log["time"])153 )154 else:155 raise ValueError("Unknown step log type '%s'" % json_log["type"])156 step.add_log(step_log)157 return step158def _unserialize_result(json_result, result):159 result.status = json_result["status"]160 # status_details for non-test results has been introduced in report version 1.1:161 result.status_details = json_result.get("status_details", None)162 result.start_time = _unserialize_time(json_result["start_time"])163 result.end_time = _unserialize_time(json_result["end_time"])164 for json_step in json_result["steps"]:165 result.add_step(_unserialize_step(json_step))166 return result167def _unserialize_node_metadata(json_node, node):168 node.tags = json_node["tags"]169 node.properties = json_node["properties"]170 node.links = [(link["url"], link["name"]) for link in json_node["links"]]171def _unserialize_test_result(json_test):172 test = TestResult(json_test["name"], json_test["description"])173 _unserialize_result(json_test, test)174 _unserialize_node_metadata(json_test, test)175 return test176def _unserialize_suite_result(json_suite):177 suite = SuiteResult(json_suite["name"], json_suite["description"])178 _unserialize_node_metadata(json_suite, suite)179 suite.start_time = _unserialize_time(json_suite["start_time"])180 suite.end_time = _unserialize_time(json_suite["end_time"])181 if "suite_setup" in json_suite:182 suite.suite_setup = Result()183 _unserialize_result(json_suite["suite_setup"], suite.suite_setup)184 for json_test in json_suite["tests"]:185 suite.add_test(_unserialize_test_result(json_test))186 if "suite_teardown" in json_suite:187 suite.suite_teardown = Result()188 _unserialize_result(json_suite["suite_teardown"], suite.suite_teardown)189 for json_sub_suite in json_suite["suites"]:190 suite.add_suite(_unserialize_suite_result(json_sub_suite))191 return suite192def _unserialize_report(json_report):193 report = Report()194 report.title = json_report["title"]195 report.info = json_report["info"]196 report.start_time = _unserialize_time(json_report["start_time"])197 report.end_time = _unserialize_time(json_report["end_time"])198 report.saving_time = _unserialize_time(json_report["generation_time"])199 report.nb_threads = json_report["nb_threads"]200 if "test_session_setup" in json_report:201 report.test_session_setup = Result()202 _unserialize_result(json_report["test_session_setup"], report.test_session_setup)203 for json_suite in json_report["suites"]:204 report.add_suite(_unserialize_suite_result(json_suite))205 if "test_session_teardown" in json_report:206 report.test_session_teardown = Result()207 _unserialize_result(json_report["test_session_teardown"], report.test_session_teardown)208 return report209def load_report_from_file(filename):210 try:211 with open(filename, "r") as fh:212 js_content = fh.read()213 except IOError as e:214 raise e # re-raise as-is215 js_content = re.sub("^" + JS_PREFIX, "", js_content)216 try:217 js = json.loads(js_content)218 except ValueError as e:219 raise ReportLoadingError(str(e))220 report_version = js.get("report_version")221 if report_version is None:222 raise ReportLoadingError("Cannot find 'report_version' in JSON")223 if report_version >= 2.0:224 raise ReportLoadingError("Incompatible report version: got %s while 1.x is supported" % report_version)225 return _unserialize_report(js)226class JsonBackend(FileReportBackend, ReportUnserializerMixin):227 def __init__(self, javascript_compatibility=True, pretty_formatting=False):228 self.javascript_compatibility = javascript_compatibility229 self.pretty_formatting = pretty_formatting230 def get_name(self):231 return "json"232 def get_report_filename(self):233 return "report.js"234 def save_report(self, filename, report):235 save_report_into_file(236 report, filename,237 javascript_compatibility=self.javascript_compatibility, pretty_formatting=self.pretty_formatting238 )239 def load_report(self, path):240 report = load_report_from_file(path)241 report.bind(self, path)...

Full Screen

Full Screen

test_cmd_top.py

Source:test_cmd_top.py Github

copy

Full Screen

...42 make_suite_result("suite1", tests=[make_test_result("test", start_time=0.1, end_time=1.0)]),43 make_suite_result("suite2", tests=[make_test_result("test", start_time=1.0, end_time=4.0)]),44 ])45 report_path = tmpdir.join("report.json").strpath46 save_report_into_file(report, report_path)47 assert main(["top-suites", report_path]) == 048 lines = cmdout.get_lines()49 assert "suite2" in lines[4]50def test_top_suites_cmd_test_run_in_progress(report_in_progress_path, cmdout):51 assert main(["top-suites", report_in_progress_path]) == 052 cmdout.dump()53 cmdout.assert_substrs_anywhere(["suite"])54def test_get_top_tests():55 report = make_report([56 make_suite_result("suite1", tests=[make_test_result("test", start_time=0.0, end_time=1.0)]),57 make_suite_result("suite2", tests=[make_test_result("test", start_time=1.0, end_time=4.0)]),58 ])59 top_suites = TopTests.get_top_tests(report, ResultFilter())60 assert len(top_suites) == 261 assert top_suites[0][0] == "suite2.test"62 assert top_suites[0][1] == "3.000s"63 assert top_suites[0][2] == "75%"64 assert top_suites[1][0] == "suite1.test"65 assert top_suites[1][1] == "1.000s"66 assert top_suites[1][2] == "25%"67def test_top_tests_cmd(tmpdir, cmdout):68 report = make_report([69 make_suite_result("suite1", tests=[make_test_result("test", start_time=0.1, end_time=1.0)]),70 make_suite_result("suite2", tests=[make_test_result("test", start_time=1.0, end_time=4.0)]),71 ])72 report_path = tmpdir.join("report.json").strpath73 save_report_into_file(report, report_path)74 assert main(["top-tests", report_path]) == 075 lines = cmdout.get_lines()76 assert "suite2.test" in lines[4]77def test_top_tests_cmd_test_run_in_progress(report_in_progress_path, cmdout):78 assert main(["top-tests", report_in_progress_path]) == 079 cmdout.dump()80 cmdout.assert_substrs_anywhere(["suite.test_2"])81def test_get_top_steps():82 report = make_report([83 make_suite_result("suite1", tests=[make_test_result(steps=[84 make_step("step1", start_time=0.0, end_time=1.0),85 make_step("step1", start_time=1.0, end_time=3.0),86 ])]),87 make_suite_result("suite2", tests=[make_test_result(steps=[88 make_step("step2", start_time=3.0, end_time=4.0)89 ])]),90 ])91 top_steps = TopSteps.get_top_steps(report, StepFilter())92 assert len(top_steps) == 293 assert top_steps[0][0] == "step1"94 assert top_steps[0][1] == "2"95 assert top_steps[0][2] == "1.000s"96 assert top_steps[0][3] == "2.000s"97 assert top_steps[0][4] == "1.500s"98 assert top_steps[0][5] == "3.000s"99 assert top_steps[0][6] == "75%"100 assert top_steps[1][0] == "step2"101 assert top_steps[1][1] == "1"102 assert top_steps[1][2] == "1.000s"103 assert top_steps[1][3] == "1.000s"104 assert top_steps[1][4] == "1.000s"105 assert top_steps[1][5] == "1.000s"106 assert top_steps[1][6] == "25%"107def test_get_top_steps_with_test_session_setup_and_grep():108 @lcc.fixture(scope="session")109 def fixt():110 lcc.set_step("mystep")111 lcc.log_info("foobar")112 @lcc.suite("suite")113 class suite:114 @lcc.test("test")115 def test(self, fixt):116 pass117 report = run_suite_class(suite, fixtures=[fixt])118 top_steps = TopSteps.get_top_steps(report, StepFilter(grep=re.compile("foobar")))119 assert len(top_steps) == 1120 assert top_steps[0][0] == "mystep"121def test_get_top_steps_filter_on_passed():122 @lcc.suite("suite")123 class suite:124 @lcc.test("test")125 def test(self):126 lcc.set_step("something ok")127 lcc.log_info("info")128 lcc.set_step("something not ok")129 lcc.log_error("error")130 report = run_suite_class(suite)131 top_steps = TopSteps.get_top_steps(report, StepFilter(passed=True))132 assert len(top_steps) == 1133 assert top_steps[0][0] == "something ok"134def test_get_top_steps_filter_on_grep():135 @lcc.suite("suite")136 class suite:137 @lcc.test("test")138 def test(self):139 lcc.set_step("something ok")140 lcc.log_info("info")141 lcc.set_step("something not ok")142 lcc.log_error("error")143 report = run_suite_class(suite)144 top_steps = TopSteps.get_top_steps(report, StepFilter(grep=re.compile("error")))145 assert len(top_steps) == 1146 assert top_steps[0][0] == "something not ok"147def test_top_steps_cmd(tmpdir, cmdout):148 report = make_report([149 make_suite_result("suite1", tests=[150 make_test_result(steps=[make_step("step1", start_time=0.1, end_time=1.0)])151 ])152 ])153 report_path = tmpdir.join("report.json").strpath154 save_report_into_file(report, report_path)155 assert main(["top-steps", report_path]) == 0156 lines = cmdout.get_lines()157 assert "step1" in lines[4]158def test_top_steps_cmd_test_run_in_progress(report_in_progress_path, cmdout):159 assert main(["top-steps", report_in_progress_path]) == 0160 cmdout.dump()...

Full Screen

Full Screen

junit.py

Source:junit.py Github

copy

Full Screen

...78 if six.PY3:79 return ET.tostring(xml_report, pretty_print=True, encoding="unicode")80 else:81 return ET.tostring(xml_report, pretty_print=True, xml_declaration=True, encoding="utf-8")82def save_report_into_file(report, filename, indent_level=DEFAULT_INDENT_LEVEL):83 content = serialize_report_as_string(report, indent_level)84 with open(filename, "w") as fh:85 fh.write(content)86class JunitBackend(FileReportBackend):87 def __init__(self):88 self.indent_level = DEFAULT_INDENT_LEVEL89 def get_name(self):90 return "junit"91 def is_available(self):92 return LXML_IS_AVAILABLE93 def get_report_filename(self):94 return "report-junit.xml"95 def save_report(self, filename, report):...

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