如何构建包装器 pytest 插件? [英] How to build a wrapper pytest plugin?

查看:59
本文介绍了如何构建包装器 pytest 插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to wrap the pytest-html plugin in the following way:

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

我能够通过实现 pytest_addoption(parser) 函数来添加选项,但卡在第二件事上......

I was able to add the option with implementing the pytest_addoption(parser) function, but got stuck on the 2nd thing...

我能做的是:实现一个钩子 frmo pytest-html.但是,我必须访问我的选项 X,才能执行该操作.问题是,pytest-html 的钩子没有将请求"对象作为参数,所以我无法访问选项值...

What I was able to do is this: implement a hook frmo pytest-html. However, I have to access my option X, in order to do what to do. The problem is, pytest-html's hook does not give the "request" object as a param, so I can't access the option value...

我可以为钩子添加额外的参数吗?或类似的东西?

Can I have additional args for a hook? or something like this?

推荐答案

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

You can attach additional data to the report object, for example via a custom wrapper around the pytest_runtest_makereport hook:

@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 的钩子:

Now the config object will be accessible via report.config in all reporting hooks, including the ones of pytest-html:

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

这篇关于如何构建包装器 pytest 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆