From 4b5ac5ed4e50d804fc20c2f2de5baf8f6aaa1fe8 Mon Sep 17 00:00:00 2001 From: "Shekhar Sorot ( MSFT )" Date: Tue, 5 Nov 2024 14:15:44 +0530 Subject: [PATCH 1/3] Update junit.py Added 3 new parameters include_passed_messages: ( Bool ) Include message and stacktrace in Junit XML output include_failed_messages: ( Bool ) Include message and stacktrace in Junit XML output include_skipped_messages: ( Bool ) Include message and stacktrace in Junit XML output Enhancements : Stacktrace now exists as an XML subproperty, making XML much cleaner. If "message" and "stacktrace" properties are empty in the TestResultMessage object, its relevant subXML objects will not be added, makes XML parsing cleaner. how to call ? Example code from runbook notifier: - type: html - type: env_stats - type: junit name: junit.lisa.xml include_subtest: true include_passed_messages: false include_failed_messages: true include_skipped_messages: true --- lisa/notifiers/junit.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/lisa/notifiers/junit.py b/lisa/notifiers/junit.py index 2dcfc74805..f65dab7f9f 100644 --- a/lisa/notifiers/junit.py +++ b/lisa/notifiers/junit.py @@ -28,6 +28,12 @@ class JUnitSchema(schema.Notifier): path: str = "lisa.junit.xml" # respect the original behavior, include subtest by default include_subtest: bool = True + # show passed case 'message' and 'stacktraces' + include_passed_messages: bool = False + # show 'message' for failed cases and 'stacktrace' + include_failed_messages: bool = True + # show 'message' for skipped cases + include_skipped_messages: bool = True class _TestSuiteInfo: @@ -291,6 +297,8 @@ def _add_test_case_result( class_name: str, elapsed: float, ) -> None: + # Creating a JunitSchema object to read the passed parameters to Junit class + runbook: JUnitSchema = cast(JUnitSchema, self.runbook) testsuite_info = self._testsuites_info.get(suite_full_name) if not testsuite_info: raise LisaException("Test suite not started.") @@ -302,18 +310,27 @@ def _add_test_case_result( if message.status == TestStatus.FAILED: failure = ET.SubElement(testcase, "failure") - failure.attrib["message"] = message.message - failure.text = message.stacktrace - + if runbook.include_failed_messages: + failure.attrib["message"] = message.message + # failure.text = message.stacktrace + if str(message.stacktrace) != "": + failure.attrib["stacktrace"] = str(message.stacktrace) testsuite_info.failed_count += 1 - elif ( - message.status == TestStatus.SKIPPED - or message.status == TestStatus.ATTEMPTED - ): + elif message.status in [TestStatus.SKIPPED, TestStatus.ATTEMPTED]: skipped = ET.SubElement(testcase, "skipped") - skipped.attrib["message"] = message.message - + if runbook.include_skipped_messages: + skipped.attrib["message"] = message.message + if str(message.stacktrace) != "": + skipped.attrib["stacktrace"] = str(message.stacktrace) + # Only add XML sub element if include_passed_messages is True in runbook + # By default, its assumed subtest is a pass + elif message.status == TestStatus.PASSED and runbook.include_passed_messages: + passed = ET.SubElement(testcase, "passed") + passed.attrib["message"] = message.message + # passed.text = message.stacktrace + if str(message.stacktrace) != "": + passed.attrib["stacktrace"] = str(message.stacktrace) testsuite_info.test_count += 1 # Write out current results to file. From 9ecf795ae51c268f6fe7762874ec032b8f2316ff Mon Sep 17 00:00:00 2001 From: "Shekhar Sorot ( MSFT )" Date: Mon, 11 Nov 2024 14:28:57 +0530 Subject: [PATCH 2/3] Update junit.py --- lisa/notifiers/junit.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lisa/notifiers/junit.py b/lisa/notifiers/junit.py index f65dab7f9f..3f9cdd33a4 100644 --- a/lisa/notifiers/junit.py +++ b/lisa/notifiers/junit.py @@ -312,8 +312,7 @@ def _add_test_case_result( failure = ET.SubElement(testcase, "failure") if runbook.include_failed_messages: failure.attrib["message"] = message.message - # failure.text = message.stacktrace - if str(message.stacktrace) != "": + if message.stacktrace: failure.attrib["stacktrace"] = str(message.stacktrace) testsuite_info.failed_count += 1 From 61177389122fb30dac440931cbc637e0ded76433 Mon Sep 17 00:00:00 2001 From: "Shekhar Sorot ( MSFT )" Date: Mon, 11 Nov 2024 14:38:04 +0530 Subject: [PATCH 3/3] Update junit.py --- lisa/notifiers/junit.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lisa/notifiers/junit.py b/lisa/notifiers/junit.py index 3f9cdd33a4..ca9e739b15 100644 --- a/lisa/notifiers/junit.py +++ b/lisa/notifiers/junit.py @@ -30,10 +30,6 @@ class JUnitSchema(schema.Notifier): include_subtest: bool = True # show passed case 'message' and 'stacktraces' include_passed_messages: bool = False - # show 'message' for failed cases and 'stacktrace' - include_failed_messages: bool = True - # show 'message' for skipped cases - include_skipped_messages: bool = True class _TestSuiteInfo: @@ -310,26 +306,24 @@ def _add_test_case_result( if message.status == TestStatus.FAILED: failure = ET.SubElement(testcase, "failure") - if runbook.include_failed_messages: - failure.attrib["message"] = message.message - if message.stacktrace: - failure.attrib["stacktrace"] = str(message.stacktrace) + failure.attrib["message"] = message.message + failure.text = message.stacktrace testsuite_info.failed_count += 1 elif message.status in [TestStatus.SKIPPED, TestStatus.ATTEMPTED]: skipped = ET.SubElement(testcase, "skipped") - if runbook.include_skipped_messages: - skipped.attrib["message"] = message.message - if str(message.stacktrace) != "": - skipped.attrib["stacktrace"] = str(message.stacktrace) + skipped.attrib["message"] = message.message + if message.stacktrace: + skipped.text = str(message.stacktrace) # Only add XML sub element if include_passed_messages is True in runbook - # By default, its assumed subtest is a pass + # By default, its assumed subtest is a pass as per Junit schema elif message.status == TestStatus.PASSED and runbook.include_passed_messages: passed = ET.SubElement(testcase, "passed") passed.attrib["message"] = message.message # passed.text = message.stacktrace - if str(message.stacktrace) != "": - passed.attrib["stacktrace"] = str(message.stacktrace) + if message.stacktrace: + passed.text = str(message.stacktrace) + testsuite_info.test_count += 1 # Write out current results to file.