简体   繁体   English

pytest-html 自定义报告

[英]Pytest-html custom report

I'm using pytest to run my tests, and using pytest html to generate a report.我正在使用 pytest 运行我的测试,并使用 pytest html 生成报告。

i'm trying to show the error\skip message in cases of failure or skip in the report, using the value form call.excinfo.value .我正在尝试使用值形式call.excinfo.value在报告失败或跳过的情况下显示错误\跳过消息。
i notice that pytest_runtest_makereport is being called multiple times, for setup , call and teardown , and since call.excinfo.value in setup and teardown is null, it is overwriting the message in the cell, and as a result the error message cell is empty.我注意到pytest_runtest_makereport被多次调用,用于setupcallteardown ,并且由于setupteardown中的call.excinfo.value是 null,它正在覆盖单元格中的消息,因此error message单元格为空.

so, i tried to update the value with the following condition report.when == "call" ,因此,我尝试使用以下条件报告更新值。当report.when == "call"时,
but i'm getting the following error when pytest_html_results_table_row is executing on setup and teardown :但是当pytest_html_results_table_rowsetupteardown上执行时,我收到以下错误:

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

here is the code that i tried:这是我尝试过的代码:

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

is there another way to show the error message in the report for failure\skipped.是否有另一种方法可以在失败\跳过的报告中显示错误消息。
ps: for tests that are passed the value should be an empty string ps:对于通过的测试,该值应该是一个空字符串

here is what i expect to achieve: expected report这是我期望实现的目标:预期报告

I've found a way to solve this issue, i hope it will help someone in the future.我找到了解决这个问题的方法,我希望它对将来的人有所帮助。
This is a workaround to handle the issue where teardown overwrites the value that was set during call phase.这是处理teardown覆盖在call阶段设置的值的问题的解决方法。

basically, after the teardown phase i'm overwriting the value in the error_message with the value that was set during the call phase.基本上,在teardown阶段之后,我将使用在call阶段设置的值覆盖error_message中的值。

Note: please take in consideration that this will show only the error message from the call phase注意:请注意,这只会显示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

see image attached report见附图片报告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM