首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pytest-html自定义报告

Pytest-html自定义报告
EN

Stack Overflow用户
提问于 2021-02-28 20:09:59
回答 1查看 1.4K关注 0票数 1

我使用pytest运行测试,并使用pytest html生成报告。

我试图在失败的情况下显示错误\跳转消息,或者使用值表单call.excinfo.value在报告中跳过。

我注意到pytest_runtest_makereport被多次调用,用于setupcallteardown,而且由于setupteardown中的call.excinfo.value为null,所以它正在覆盖单元中的消息,因此error message单元是空的。

因此,我尝试用以下条件report.when == "call"更新该值,

但是当pytest_html_results_table_rowsetupteardown上执行时,我得到了以下错误

AttributeError: 'TestReport' object has no attribute 'error_message'

下面是我尝试过的代码:

代码语言:javascript
复制
# in conftest.py
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(1, html.th('Error Message'))


@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.error_message))



@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    if report.when == "call" and report.skipped or report.failed:
        report.error_message = str(call.excinfo.value) if call.excinfo else ""

是否有另一种方式在报告中显示错误消息,请跳过失败\。

p.s:对于通过的测试,值应该是空字符串

以下是我期望实现的目标:预期报告

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-03 11:08:38

我已经找到了解决这个问题的方法,我希望它能对将来的人有所帮助。

这是一个解决方法,用于处理teardown覆盖在call阶段设置的值的问题。

基本上,在teardown阶段之后,我将用在call阶段期间设置的值覆盖error_message中的值。

注意:请注意,这将只显示来自call阶段的错误消息。

代码语言:javascript
复制
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.error_message = ""


    # set a report attribute for each phase of a call, which can be "setup", "call", "teardown"
    setattr(item, "rep_" + report.when, report)
    report.error_message = str(call.excinfo.value) if call.excinfo else ""
    if report.when == "teardown":
        # retrieving the error messages from the call phase
        report.error_message = item.rep_call.error_message

见附于报告的图像

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66413406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档