如何构建包装器pytest插件?

2024-06-07 15:11:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用以下方式包装pytest html插件:

  1. 添加一个选项X
  2. 给定选项X,从报告中删除数据

我可以通过实现pytest_addoption(parser)函数来添加选项,但在第二件事上遇到了麻烦

我能做的是:实现一个hook frmo pytest html。但是,我必须访问我的选项X,以便执行操作。问题是,pytest html的钩子没有将“request”对象作为参数提供,因此我无法访问选项值

我可以有一个钩子的附加参数吗?还是像这样


Tags: 数据函数插件parser参数pytestrequesthtml
1条回答
网友
1楼 · 发布于 2024-06-07 15:11:31

您可以将其他数据附加到报表对象,例如通过围绕pytest_runtest_makereport钩子的自定义包装器:

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.config = item.config

现在config对象可以通过所有报告挂钩中的report.config访问,包括pytest-html的挂钩:

def pytest_html_report_title(report):
    """ Called before adding the title to the report """
    assert report.config is not None

相关问题 更多 >

    热门问题