要使pytest-html单个html报告结果文件的所有行都默认为折叠状态,可以通过自定义pytest插件来实现。下面是一个实现的示例:
pytest_custom_plugin.py
。import pytest
from _pytest.config import Config
from _pytest.reports import TestReport
from _pytest.terminal import TerminalReporter
pytest.Plugin
:class CustomPlugin(pytest.Plugin):
def __init__(self):
super().__init__()
self.terminal_reporter = None
def pytest_configure(self, config: Config):
self.terminal_reporter = config.pluginmanager.getplugin("terminalreporter")
def pytest_html_report_title(self, report: TestReport):
if self.terminal_reporter:
self.terminal_reporter.write_line("Generating HTML report...")
def pytest_html_results_table_header(self, cells):
cells.insert(1, html.th("Details", class_="sortable", col="details"))
def pytest_html_results_table_row(self, report: TestReport, cells):
cells.insert(1, html.td(report.longreprtext, class_="col-details"))
def pytest_html_results_table_cell(self, report: TestReport, cell):
if cell.get("class") == "col-details":
cell["colspan"] = str(len(cell.parent.contents) - 1)
def pytest_html_report_table_row(self, report: TestReport, cells):
if report.passed:
cells.insert(1, html.td("Passed", class_="col-result"))
elif report.failed:
cells.insert(1, html.td("Failed", class_="col-result"))
elif report.skipped:
cells.insert(1, html.td("Skipped", class_="col-result"))
elif report.error:
cells.insert(1, html.td("Error", class_="col-result"))
else:
cells.insert(1, html.td("Unknown", class_="col-result"))
def pytest_html_report_table_cell(self, report: TestReport, cell):
if cell.get("class") == "col-result":
cell["colspan"] = str(len(cell.parent.contents) - 1)
def pytest_html_report_title(report):
return "Custom HTML Report"
def pytest_configure(config):
config.pluginmanager.register(CustomPlugin())
--html=<报告文件路径>
参数生成HTML报告,例如:pytest --html=report.html
生成的HTML报告中,所有行都将默认为折叠状态。
请注意,以上示例是一个简单的实现,仅供参考。根据具体需求,你可能需要进一步调整和完善插件的功能。
领取专属 10元无门槛券
手把手带您无忧上云