import datetime
class ReportGenerator:
def __init__(self):
self.title = ""
self.subtitle = "Main Part of Test Case"
self.column_names = []
self.data = []
self.html_report=""
def generate_html_report(self):
# 检查是否有失败的测试结果
test_status = self._determine_test_status()
# 创建 HTML 内容
html_content = self._create_html_structure()
# 添加表格头
html_content += self._add_table_header()
# 添加数据行
html_content += self._add_data_rows()
# 添加测试状态标记
html_content += self._add_test_status(test_status)
# 完成 HTML 结构
html_content += """
</tbody>
</table>
</div>
</body>
</html>
"""
return html_content
def _determine_test_status(self):
"""确定测试是否通过"""
for row in self.data:
if row[-1].lower() == "fail":
return "fail"
return "pass"
def _create_html_structure(self):
"""创建基本的 HTML 结构"""
return """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}}
.report-container {{
width: 80%;
margin: 20px auto;
}}
table {{
width: 100%;
border-collapse: collapse;
margin-bottom: 30px;
}}
th, td {{
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}}
th {{
background-color: #f2f2f2;
}}
tr:nth-child(even) {{
background-color: #f9f9f9;
}}
.pass {{
background-color: #d4edda;
color: #155724;
}}
.fail {{
background-color: #f8d7da;
color: #721c24;
}}
.test-status {{
text-align: center;
font-size: 24px;
font-weight: bold;
padding: 10px;
border-radius: 5px;
margin-top: 20px;
}}
.status-pass {{
background-color: #d4edda;
color: #155724;
}}
.status-fail {{
background-color: #f8d7da;
color: #721c24;
}}
/* 添加标题居中的样式 */
h1 {{
text-align: center;
}}
h2 {{
text-align: center;
}}
</style>
</head>
<body>
<div class="report-container">
<h1>{title}</h1>
<h2>{subtitle}</h2>
<table>
<thead>
""".format(title=self.title, subtitle=self.subtitle)
def _add_table_header(self):
"""添加表格列名"""
header = "<tr>"
for column_name in self.column_names:
header += f"<th>{column_name}</th>"
return header + "</tr>"
def _add_data_rows(self):
"""添加表格数据行"""
data_rows = ""
for row in self.data:
data_rows += "<tr>"
for cell_data in row:
if cell_data.lower() == "pass":
data_rows += f'<td class="pass">{cell_data}</td>'
elif cell_data.lower() == "fail":
data_rows += f'<td class="fail">{cell_data}</td>'
else:
data_rows += f"<td>{cell_data}</td>"
data_rows += "</tr>"
return data_rows
def _add_test_status(self, test_status):
"""添加测试状态标记"""
if test_status == "pass":
return """
<div class="test-status status-pass">
Test Pass
</div>
"""
else:
return """
<div class="test-status status-fail">
Test Fail
</div>
"""
def Set_TestCase_Name(self, title):
self.title = title
return self.title
def Set_TestCase_column_names(self, column_names):
self.column_names = column_names
return self.column_names
def Set_TestCase_Step_Style(self, swc, pdu, signal, expected_value, actual_value, result):
step = [datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
swc,
pdu,
signal,
expected_value,
actual_value,
result]
self.data.append(step)
def Generate_Report_Style(self):
self.html_report=self.generate_html_report()
with open(self.title+".html", "w", encoding="utf-8") as file:
file.write(self.html_report)
1、新建一个Html_Report.py,Test_System.py