【发布时间】:2021-03-03 19:13:53
【问题描述】:
我正在使用 pytest 运行我的测试,并使用 pytest html 生成报告。
我正在尝试使用值形式call.excinfo.value 在报告中显示失败或跳过的错误\跳过消息。
我注意到pytest_runtest_makereport 被多次调用,对于setup、call 和teardown,并且由于call.excinfo.value 在setup 和teardown 中为空,它正在覆盖单元格中的消息,结果error message 单元格为空。
所以,我尝试使用以下条件更新值report.when == "call",
但是当pytest_html_results_table_row 在setup 和teardown 上执行时出现以下错误:
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