-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_report.py
71 lines (56 loc) · 2.23 KB
/
generate_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import re
import xml.etree.ElementTree as ET
TEST_DIR = "build/test-results/test"
def generate_report_table(headers, data):
header_row = "| " + " | ".join(headers) + " |\n"
separator_row = "| " + " | ".join(["---"] * len(headers)) + " |\n"
f.write(header_row)
f.write(separator_row)
for row in data:
row_bytes = [
cell.encode("utf-8").decode("utf-8") if isinstance(cell, str) else str(cell)
for cell in row
]
f.write("| " + " | ".join(row_bytes) + " |\n")
def pretty_test_name(test_name):
test_name = re.sub(r"T(\d+)", r"Task\1:", test_name)
test_name = re.sub(r"(?<=[a-z])(?=[A-Z])", r" ", test_name)
test_name = test_name.replace("test", "")
test_name = test_name.replace("Test", "")
test_name = test_name.replace("()", "")
return test_name.strip()
def pretty_message(message):
message_parts = message.split(":")
error_type = message_parts[0]
error_message = message_parts[1]
if "AssertionFailedError" in error_type:
error_message = error_message.split("==>")[0]
if "Exception" in error_type:
error_message = "⚠️ " + error_type.split(".")[-1]
return error_message
def extract_task_number(filename):
match = re.search(r"T(\d+)", filename)
return int(match.group(1)) if match else float("inf")
f = open("report.md", "w", encoding="utf-8")
f.write("\n# Report\n\n")
headers = ["Test", "Status", "Reason"]
filenames = [filename for filename in os.listdir(TEST_DIR) if filename.endswith(".xml")]
filenames.sort(key=extract_task_number)
for filename in filenames:
data = []
title = filename.split(".")[-2]
f.write("## " + pretty_test_name(title) + "\n\n")
root = ET.parse(os.path.join(TEST_DIR, filename))
for e in root.findall(".//testcase"):
failure = e.find("failure")
if failure is not None:
test_name = pretty_test_name(e.get("name"))
message = pretty_message(failure.get("message"))
data.append([test_name, "❌ Failed", message])
else:
test_name = pretty_test_name(e.get("name"))
message = "Passed"
data.append([test_name, "✅ Passed", "-"])
generate_report_table(headers, data)
f.close()