问题描述
我使用 pytest 运行我的测试,并使用 pytest html 生成报告。
我正在尝试使用值形式 call.excinfo.value
在失败或跳过报告的情况下显示错误\跳过消息。
我注意到 pytest_runtest_makereport
被多次调用,对于 setup
、call
和 teardown
,并且因为 call.excinfo.value
和 {{1} 中的 setup
}} 为空,它会覆盖单元格中的消息,因此 teardown
单元格为空。
因此,我尝试使用以下条件更新值 error message
,
但是当 report.when == "call"
在 pytest_html_results_table_row
和 setup
上执行时,我收到以下错误:
teardown
这是我试过的代码:
AttributeError: 'TestReport' object has no attribute 'error_message'
是否有另一种方法可以在失败\跳过的报告中显示错误消息。
p.s:对于通过的测试,该值应该是一个空字符串
这是我期望实现的目标: expected report
解决方法
我已经找到了解决这个问题的方法,希望以后能帮到大家。
这是一种解决 teardown
覆盖在 call
阶段设置的值的问题的解决方法。
基本上,在 teardown
阶段之后,我用 error_message
阶段设置的值覆盖 call
中的值。
注意:请注意,这只会显示 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