【问题标题】:pytest-html extras customizing code understandingpytest-html extras 自定义代码理解
【发布时间】: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


    【解决方案1】:

    尝试回答尽可能多的行。

    Pytest 使用generators 迭代报告步骤。

    函数 pytest_runtest_makereport 迭代每个 result.outcome,根据 pytest 是 follows:'collect'、'setup'、'call' 和 'teardown'。

    get_resultpytest 实现其挂钩的方式。

    关于failedxfail(预计会失败)的混淆是您定义测试失败的方式:如果它被跳过但预计会失败,或者如果它失败但预计不会失败,则为错误。

    事情与关于:空白也可以是bug

    您想在 if 语句上使用的不是呼叫信息,而是 report

    if report.failed:
        do_stuff()
    if report.passed:
        do_stuff_different()
    
    

    获取有关代码及其上下文的更多信息的一种方法是使用 breakpoint() 对其进行调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多