|
| 1 | +import json |
| 2 | +import xml.etree.ElementTree as ET |
| 3 | + |
| 4 | +def sanitize_classname(filepath): |
| 5 | + if not filepath: |
| 6 | + return "default" |
| 7 | + return filepath.replace("\\", ".").replace("/", ".").removesuffix(".py") |
| 8 | + |
| 9 | +def sanitize_test_name(test): |
| 10 | + return test.get("test") or test.get("nodeid", "unknown").split("::")[-1] |
| 11 | + |
| 12 | +def convert_json_to_junit_xml(json_path, xml_path): |
| 13 | + with open(json_path, "r", encoding="utf-8") as f: |
| 14 | + test_results = json.load(f) |
| 15 | + |
| 16 | + testsuite = ET.Element("testsuite", { |
| 17 | + "name": "Test Suite", |
| 18 | + "tests": str(len(test_results)), |
| 19 | + }) |
| 20 | + |
| 21 | + for test in test_results: |
| 22 | + testcase = ET.SubElement(testsuite, "testcase", { |
| 23 | + "classname": sanitize_classname(test.get("file")), |
| 24 | + "name": sanitize_test_name(test), |
| 25 | + "time": str(test.get("duration", 0)), |
| 26 | + }) |
| 27 | + |
| 28 | + if test.get("timestamp"): |
| 29 | + testcase.set("timestamp", test["timestamp"]) |
| 30 | + if test.get("line") is not None: |
| 31 | + testcase.set("line", str(test["line"])) |
| 32 | + if test.get("worker"): |
| 33 | + testcase.set("worker", test["worker"]) |
| 34 | + if test.get("flaky") is not None: |
| 35 | + testcase.set("flaky", str(test["flaky"]).lower()) |
| 36 | + |
| 37 | + # Add status tag |
| 38 | + status = test.get("status", "").lower() |
| 39 | + if status == "failed": |
| 40 | + failure = ET.SubElement(testcase, "failure", { |
| 41 | + "message": test.get("error") or "Test failed", |
| 42 | + "type": "AssertionError" |
| 43 | + }) |
| 44 | + failure.text = test.get("stderr", "") |
| 45 | + elif status == "skipped": |
| 46 | + ET.SubElement(testcase, "skipped") |
| 47 | + |
| 48 | + # Add stdout/stderr |
| 49 | + if test.get("stdout"): |
| 50 | + system_out = ET.SubElement(testcase, "system-out") |
| 51 | + system_out.text = test["stdout"] |
| 52 | + if test.get("stderr"): |
| 53 | + system_err = ET.SubElement(testcase, "system-err") |
| 54 | + system_err.text = test["stderr"] |
| 55 | + |
| 56 | + # Add <properties> |
| 57 | + properties = ET.SubElement(testcase, "properties") |
| 58 | + |
| 59 | + # Markers |
| 60 | + for marker in test.get("markers", []): |
| 61 | + ET.SubElement(properties, "property", { |
| 62 | + "name": "marker", |
| 63 | + "value": marker |
| 64 | + }) |
| 65 | + |
| 66 | + # Links |
| 67 | + for link in test.get("links", []): |
| 68 | + ET.SubElement(properties, "property", { |
| 69 | + "name": "link", |
| 70 | + "value": link |
| 71 | + }) |
| 72 | + |
| 73 | + # Screenshot |
| 74 | + if test.get("screenshot"): |
| 75 | + ET.SubElement(properties, "property", { |
| 76 | + "name": "screenshot", |
| 77 | + "value": test["screenshot"] |
| 78 | + }) |
| 79 | + |
| 80 | + # Logs |
| 81 | + for log in test.get("logs", []): |
| 82 | + ET.SubElement(properties, "property", { |
| 83 | + "name": "log", |
| 84 | + "value": str(log) |
| 85 | + }) |
| 86 | + |
| 87 | + # Write XML to file |
| 88 | + tree = ET.ElementTree(testsuite) |
| 89 | + tree.write(xml_path, encoding="utf-8", xml_declaration=True) |
| 90 | + print(f"XML report generated at: {xml_path}") |
0 commit comments