|
| 1 | +# Copyright (c) 2024 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 | +""" |
| 13 | +This script generates a summary table in Markdown format from an XML report generated by pytest. |
| 14 | +
|
| 15 | +Usage in GitHub workflow: |
| 16 | + - name: Test Summary |
| 17 | + if: ${{ !cancelled() }} |
| 18 | + run: | |
| 19 | + python .github/scripts/generate_examples_summary.py pytest-results.xml >> $GITHUB_STEP_SUMMARY |
| 20 | +""" |
| 21 | + |
| 22 | +import sys |
| 23 | +import xml.etree.ElementTree as ET |
| 24 | + |
| 25 | +# Load the XML report generated by pytest |
| 26 | +xml_file = sys.argv[1] |
| 27 | + |
| 28 | +try: |
| 29 | + tree = ET.parse(xml_file) |
| 30 | +except FileNotFoundError: |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | +root = tree.getroot() |
| 34 | + |
| 35 | +# Build the summary table in Markdown format |
| 36 | +table_lines = [] |
| 37 | +table_lines.append("| Test Name | Status | Time | Message |") |
| 38 | +table_lines.append("|:----------|:------:|-----:|:--------|") |
| 39 | + |
| 40 | +# Iterate over test cases |
| 41 | +for testcase in root.findall(".//testcase"): |
| 42 | + test_name = testcase.get("name") |
| 43 | + time_duration = float(testcase.get("time", "0")) |
| 44 | + message = "" |
| 45 | + if testcase.find("failure") is not None: |
| 46 | + status = "$${\color{red}Failed}$$" |
| 47 | + message = testcase.find("failure").get("message", "") |
| 48 | + elif testcase.find("error") is not None: |
| 49 | + status = "$${\color{red}Error}$$" |
| 50 | + elif testcase.find("skipped") is not None: |
| 51 | + status = "$${\color{orange}Skipped}$$" |
| 52 | + message = testcase.find("skipped").get("message", "") |
| 53 | + else: |
| 54 | + status = "$${\color{green}Ok}$$" |
| 55 | + |
| 56 | + # Append each row to the table |
| 57 | + table_lines.append(f"| {test_name} | {status} | {time_duration:.0f} | {message} |") |
| 58 | + |
| 59 | +print("\n".join(table_lines)) |
0 commit comments