我使用pytest运行测试,并使用pytest html生成报告。
我试图在失败的情况下显示错误\跳转消息,或者使用值表单call.excinfo.value在报告中跳过。
我注意到pytest_runtest_makereport被多次调用,用于setup、call和teardown,而且由于setup和teardown中的call.excinfo.value为null,所以它正在覆盖单元中的消息,因此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 ""是否有另一种方式在报告中显示错误消息,请跳过失败\。
p.s:对于通过的测试,值应该是空字符串
以下是我期望实现的目标:预期报告
发布于 2021-03-03 11:08:38
我已经找到了解决这个问题的方法,我希望它能对将来的人有所帮助。
这是一个解决方法,用于处理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见附于报告的图像
https://stackoverflow.com/questions/66413406
复制相似问题