python - 如何构建包装 pytest 插件?
问题描述
我想用以下方式包装 pytest-html 插件:
- 添加选项 X
- 给定选项 X,从报告中删除数据
我能够通过实现该pytest_addoption(parser)
功能添加选项,但被困在第二件事上......
我能做的是:实现一个钩子 frmo pytest-html。但是,我必须访问我的选项 X,才能执行该操作。问题是,pytest-html 的钩子没有将“请求”对象作为参数,所以我无法访问选项值......
我可以为钩子添加额外的参数吗?或类似的东西?
解决方案
您可以将其他数据附加到报告对象,例如通过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
推荐阅读
- javascript - ReactJS 承诺不会兑现
- javascript - JSON输入错误的意外结束,但似乎该方法已经运行?
- javascript - 将第一条消息从 chrome 扩展后台脚本发送到内容脚本?
- c++ - 仅使用一个类定义重载模板化类模板参数
- android - 添加无法解决的sceneform 1.16时出错
- c# - WinForms (C#) 仅通过 Timer 每隔一个循环更新 PictureBoxes
- python - 白平衡和颜色转换
- android - 如何在没有谷歌地图 API 的情况下在我的应用程序中获取经度和纬度?
- cmake - CMake 函数“未在此范围内声明”错误
- php - 使用nodejs但不使用php下载多个文件的不一致