【发布时间】:2023-02-04 03:02:12
【问题描述】:
我正在尝试使用 pytest-html 插件自定义 pytest 的 report.html。
我搜索了很多网站(包括pytest-html文档),发现下面的代码是常用的。(代码在coftest.py中)
(https://pytest-html.readthedocs.io/en/latest/user_guide.html#extra-content)
@pytest.hookimpl(hookwrapper = True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.outcome == "call":
#always add url to report
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
extra.append(pytest_html.extras.url("http://www.google.com/"))
extra.append(pytest_html.extras.text('Hi', name = 'TEXT'))
# only add additional html on failure
# extra.append(pytest_html.extras.html("<div>Additional HTML</div>"))
report.extra = extra
但是,我不知道每一行。
没有人解释这条线的实际作用。
为什么脚本分配屈服没有任何变量(例如 yield 1)的结果关键字,以及 yield.get_result() 实际上做了什么?
另外,我不知道 xfail("wasxfail")。
我发现在pytest运行时@pytest.xfail使得测试函数失败,但我认为这与上面的代码无关。
为什么我们不使用“失败”而不是“xfail”?
不管怎样,我需要的是
第一的,每行的含义及其作用。
第二,我想根据通过/失败在 report.html 中设置不同的消息。
我试过python report.outcome == 'failed', report.outcome == 'passed'划分条件,但是没有用。
第三,当添加文本而不是 url 时,它成为标记并有助于重定向包含文本的页面。
但是,如果我单击 html 中的页面,它会打开关于:空白页面不是所需的页面。
使用右键单击并在新选项卡中打开重定向到所需的选项卡。
欢迎任何帮助。谢谢。
【问题讨论】:
标签: pytest pytest-html pytest-html-reporter