|
| 1 | +#!/bin/env python3 |
| 2 | +import argparse |
| 3 | +import glob |
| 4 | +import xml.etree.ElementTree as ET |
| 5 | + |
| 6 | +def parse_arguments(): |
| 7 | + parser = argparse.ArgumentParser(description="Parse JUnit XML test results and generate a summary.") |
| 8 | + parser.add_argument("--path-to-test-results", required=True, help="Path pattern to JUnit XML test result files. Supports wildcards (e.g., results/**/*.xml).") |
| 9 | + parser.add_argument("--header", required=True, help="The title of the summary output.") |
| 10 | + return parser.parse_args() |
| 11 | + |
| 12 | +def parse_junit_xml(file): |
| 13 | + try: |
| 14 | + tree = ET.parse(file) |
| 15 | + root = tree.getroot() |
| 16 | + |
| 17 | + # Extract main test summary from the first-level testsuite (overall summary) |
| 18 | + main_suite = root.find("./testsuite[@name='']") |
| 19 | + if main_suite is None: |
| 20 | + main_suite = root.find("./testsuite") |
| 21 | + |
| 22 | + total_tests = int(main_suite.attrib.get("tests", 0)) |
| 23 | + total_failures = int(main_suite.attrib.get("failures", 0)) |
| 24 | + total_errors = int(main_suite.attrib.get("errors", 0)) |
| 25 | + total_skipped = int(main_suite.attrib.get("skipped", 0)) |
| 26 | + total_time = float(main_suite.attrib.get("time", 0)) |
| 27 | + passed_tests = total_tests - total_failures - total_errors - total_skipped |
| 28 | + |
| 29 | + failures = [] |
| 30 | + # Extract failures from nested test suites |
| 31 | + for ts in root.findall(".//testsuite[@name!='']"): |
| 32 | + for tc in ts.findall("testcase[failure]"): |
| 33 | + class_name = tc.attrib.get("classname", "Unknown") |
| 34 | + test_name = tc.attrib.get("name", "Unknown") |
| 35 | + file_path = tc.attrib.get("file", "Unknown") |
| 36 | + line = tc.attrib.get("line", "Unknown") |
| 37 | + failure_message = tc.find("failure").text.strip() if tc.find("failure") is not None else "Unknown" |
| 38 | + failure_message = failure_message.replace("\n", "<br>") # Convert newlines to Markdown format |
| 39 | + failures.append((class_name, test_name, f"{file_path}:{line}", failure_message)) |
| 40 | + |
| 41 | + return file, passed_tests, total_failures, total_skipped, total_errors, total_time, failures |
| 42 | + except Exception as e: |
| 43 | + print(f"Error processing {file}: {e}") |
| 44 | + return file, 0, 0, 0, 0, 0.0, [] |
| 45 | + |
| 46 | +def main(): |
| 47 | + args = parse_arguments() |
| 48 | + files = glob.glob(args.path_to_test_results, recursive=True) |
| 49 | + |
| 50 | + print(f"## {args.header}\n") |
| 51 | + print("| Status | File | ✅ Passed | ❌ Failed | ⚠ Skipped | 🔍 Errors | ⏱ Time (s) |") |
| 52 | + print("|--------|------|---------|---------|---------|---------|---------|") |
| 53 | + |
| 54 | + all_failures = [] |
| 55 | + for file in files: |
| 56 | + file, passed, failed, skipped, errors, total_time, failures = parse_junit_xml(file) |
| 57 | + status = "✅" if failed == 0 else "❌" |
| 58 | + print(f"| {status} | {file} | {passed} | {failed} | {skipped} | {errors} | {total_time:.3f} |") |
| 59 | + all_failures.extend(failures) |
| 60 | + |
| 61 | + if all_failures: |
| 62 | + print("<details>\n<summary>Failure Details</summary>\n") |
| 63 | + print("### Failure Details\n") |
| 64 | + print("| Test Class | Test Name | File:Line | Failure Message |") |
| 65 | + print("|------------|----------|----------|----------------|") |
| 66 | + for class_name, test_name, file_line, failure_message in all_failures: |
| 67 | + print(f"| {class_name} | {test_name} | {file_line} | {failure_message} |") |
| 68 | + print("\n</details>\n") |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments