【问题标题】:Pytest-html custom reportpytest-html 自定义报告
【发布时间】:2021-03-03 19:13:53
【问题描述】:

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

我正在尝试使用值形式call.excinfo.value 在报告中显示失败或跳过的错误\跳过消息。
我注意到pytest_runtest_makereport 被多次调用,对于setupcallteardown,并且由于call.excinfo.valuesetupteardown 中为空,它正在覆盖单元格中的消息,结果error message 单元格为空。

所以,我尝试使用以下条件更新值report.when == "call"
但是当pytest_html_results_table_rowsetupteardown 上执行时出现以下错误:

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

这是我尝试过的代码:

# 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 ""
   

是否有另一种方法可以在失败\跳过的报告中显示错误消息。
ps:对于通过的测试,该值应该是一个空字符串

这是我期望实现的目标: expected report

【问题讨论】:

    标签: python report pytest pytest-html


    【解决方案1】:

    我已经找到了解决此问题的方法,希望它对将来的人有所帮助。
    这是解决teardown 覆盖在call 阶段设置的值的问题的解决方法。

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

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

    @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
    
    

    见附图report

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 2022-10-08
      相关资源
      最近更新 更多