|
9 | 9 | # See the License for the specific language governing permissions and
|
10 | 10 | # limitations under the License.
|
11 | 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 | 12 | import sys
|
23 | 13 | import xml.etree.ElementTree as ET
|
24 | 14 |
|
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)) |
| 15 | + |
| 16 | +def parse_xml_report(xml_file) -> None: |
| 17 | + """ |
| 18 | + Parse the XML report generated by pytest. |
| 19 | +
|
| 20 | + :param xml_file: Path to the XML report file |
| 21 | + :return: None |
| 22 | + """ |
| 23 | + try: |
| 24 | + tree = ET.parse(xml_file) |
| 25 | + except FileNotFoundError: |
| 26 | + sys.exit(1) |
| 27 | + |
| 28 | + root = tree.getroot() |
| 29 | + |
| 30 | + # Build the summary table in Markdown format |
| 31 | + table_lines = [] |
| 32 | + table_lines.append("| Test Name | Status | Time | Message |") |
| 33 | + table_lines.append("|:----------|:------:|-----:|:--------|") |
| 34 | + |
| 35 | + # Iterate over test cases |
| 36 | + for testcase in root.findall(".//testcase"): |
| 37 | + test_name = testcase.get("name") |
| 38 | + time_duration = float(testcase.get("time", "0")) |
| 39 | + message = "" |
| 40 | + if testcase.find("failure") is not None: |
| 41 | + status = "$${\color{red}Failed}$$" |
| 42 | + message = testcase.find("failure").get("message", "") |
| 43 | + elif testcase.find("error") is not None: |
| 44 | + status = "$${\color{red}Error}$$" |
| 45 | + elif testcase.find("skipped") is not None: |
| 46 | + status = "$${\color{orange}Skipped}$$" |
| 47 | + message = testcase.find("skipped").get("message", "") |
| 48 | + else: |
| 49 | + status = "$${\color{green}Ok}$$" |
| 50 | + |
| 51 | + # Append each row to the table |
| 52 | + if message: |
| 53 | + message = message.splitlines()[0][:60] |
| 54 | + table_lines.append(f"| {test_name} | {status} | {time_duration:.0f} | {message} |") |
| 55 | + |
| 56 | + if len(table_lines) > 2: |
| 57 | + # Print the summary table only if there are test cases |
| 58 | + print("\n".join(table_lines)) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + """ |
| 63 | + This script generates a summary table in Markdown format from an XML report generated by pytest. |
| 64 | +
|
| 65 | + Usage in GitHub workflow: |
| 66 | + - name: Test Summary |
| 67 | + if: ${{ !cancelled() }} |
| 68 | + run: | |
| 69 | + python .github/scripts/generate_examples_summary.py pytest-results.xml >> $GITHUB_STEP_SUMMARY |
| 70 | + """ |
| 71 | + try: |
| 72 | + parse_xml_report(sys.argv[1]) |
| 73 | + except Exception as e: |
| 74 | + print(f"Error: {e}") |
0 commit comments