生成 Html报告

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 += """
                    
                
            
""" 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 """ {title}

{title}

{subtitle}

""".format(title=self.title, subtitle=self.subtitle) def _add_table_header(self): """添加表格列名""" header = "" for column_name in self.column_names: header += f"" return header + "" def _add_data_rows(self): """添加表格数据行""" data_rows = "" for row in self.data: data_rows += "" for cell_data in row: if cell_data.lower() == "pass": data_rows += f'' elif cell_data.lower() == "fail": data_rows += f'' else: data_rows += f"" data_rows += "" return data_rows def _add_test_status(self, test_status): """添加测试状态标记""" if test_status == "pass": return """
Test Pass
""" else: return """
Test Fail
""" 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

你可能感兴趣的:(python,python)

{column_name}
{cell_data}{cell_data}{cell_data}