首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使pytest-html单个html报告结果文件的所有行都默认为折叠状态?

要使pytest-html单个html报告结果文件的所有行都默认为折叠状态,可以通过自定义pytest插件来实现。下面是一个实现的示例:

  1. 创建一个pytest插件文件,例如pytest_custom_plugin.py
  2. 在插件文件中导入必要的模块和函数:
代码语言:python
复制
import pytest
from _pytest.config import Config
from _pytest.reports import TestReport
from _pytest.terminal import TerminalReporter
  1. 定义一个自定义的pytest插件类,继承自pytest.Plugin
代码语言:python
复制
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)
  1. 在插件文件的末尾添加以下代码,注册插件:
代码语言:python
复制
def pytest_html_report_title(report):
    return "Custom HTML Report"

def pytest_configure(config):
    config.pluginmanager.register(CustomPlugin())
  1. 运行pytest时,使用--html=<报告文件路径>参数生成HTML报告,例如:
代码语言:bash
复制
pytest --html=report.html

生成的HTML报告中,所有行都将默认为折叠状态。

请注意,以上示例是一个简单的实现,仅供参考。根据具体需求,你可能需要进一步调整和完善插件的功能。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

扫码

添加站长 进交流群

领取专属 10元无门槛券

手把手带您无忧上云

扫码加入开发者社群

相关资讯

热门标签

活动推荐

    运营活动

    活动名称
    广告关闭
    领券