|
| 1 | +# Copyright (c) 2025 Intel Corporation |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import sys |
| 13 | + |
| 14 | +from defusedxml import ElementTree as ET |
| 15 | + |
| 16 | + |
| 17 | +def parse_xml_report(xml_file) -> None: |
| 18 | + """ |
| 19 | + Parse the XML report generated by pytest. |
| 20 | +
|
| 21 | + :param xml_file: Path to the XML report file |
| 22 | + :return: None |
| 23 | + """ |
| 24 | + try: |
| 25 | + tree = ET.parse(xml_file) |
| 26 | + except FileNotFoundError: |
| 27 | + sys.exit(1) |
| 28 | + |
| 29 | + root = tree.getroot() |
| 30 | + |
| 31 | + # Build the summary table in Markdown format |
| 32 | + table_lines = [] |
| 33 | + table_lines.append("| Test Name | Status | Time | Message |") |
| 34 | + table_lines.append("|:----------|:------:|-----:|:--------|") |
| 35 | + |
| 36 | + # Iterate over test cases |
| 37 | + for testcase in root.findall(".//testcase"): |
| 38 | + test_name = testcase.get("name") |
| 39 | + time_duration = float(testcase.get("time", "0")) |
| 40 | + message = "" |
| 41 | + if testcase.find("failure") is not None: |
| 42 | + status = "$${\color{red}Failed}$$" |
| 43 | + message = testcase.find("failure").get("message", "") |
| 44 | + elif testcase.find("error") is not None: |
| 45 | + status = "$${\color{red}Error}$$" |
| 46 | + elif testcase.find("skipped") is not None: |
| 47 | + if "xfail" in testcase.find("skipped").get("type", ""): |
| 48 | + status = "$${\color{orange}xfail}$$" |
| 49 | + message = testcase.find("skipped").get("message", "") |
| 50 | + else: |
| 51 | + status = "$${\color{yellow}Skipped}$$" |
| 52 | + message = testcase.find("skipped").get("message", "") |
| 53 | + else: |
| 54 | + status = "$${\color{green}Ok}$$" |
| 55 | + |
| 56 | + # Append each row to the table |
| 57 | + if message: |
| 58 | + message = message.splitlines()[0][:60] |
| 59 | + table_lines.append(f"| {test_name} | {status} | {time_duration:.0f} | {message} |") |
| 60 | + |
| 61 | + if len(table_lines) > 2: |
| 62 | + # Print the summary table only if there are test cases |
| 63 | + print("\n".join(table_lines)) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + """ |
| 68 | + This script generates a summary table in Markdown format from an XML report generated by pytest. |
| 69 | +
|
| 70 | + Usage in GitHub workflow: |
| 71 | + - name: Test Summary |
| 72 | + if: ${{ !cancelled() }} |
| 73 | + run: | |
| 74 | + python .github/scripts/generate_examples_summary.py pytest-results.xml >> $GITHUB_STEP_SUMMARY |
| 75 | + """ |
| 76 | + try: |
| 77 | + parse_xml_report(sys.argv[1]) |
| 78 | + except Exception as e: |
| 79 | + print(f"Error: {e}") |
0 commit comments