pytest-html报告自定义字段

from datetime import datetime
from py.xml import html
import pytest
import time
import pytest
from py.xml import html

def pytest_html_report_title(report):
   report.title = "接口自动化测试报告" #修改报告名称

# def pytest_html_results_summary(prefix, summary, postfix):
#     prefix.extend([html.p("foo: bar")])

def pytest_configure(config):
    config._metadata['测试人员'] = 'emily' #添加环境项

runtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

print(runtime)

def pytest_html_results_table_header(cells):
    cells.insert(0, html.th('用例编号'))
    cells.insert(1, html.th('所属模块'))
    cells.insert(2, html.th('用例名称'))
    cells.insert(3, html.th('接口路径'))
    cells.insert(5, html.th('执行时间', class_='sortable time', col='time'))
    cells.pop(6)
    cells.pop()

def pytest_html_results_table_row(report, cells):
    url = 'http://xxx.com'
    testnode = report.nodeid.encode("utf-8").decode("unicode_escape")  #获取测试节点(测试数据格式列表中有元祖,元祖有列表)
    caseid = testnode.split('-')[3]  #获取测试节点中的部分内容
    cells.insert(0, html.td(caseid))
    module = testnode.split('-')[2]
    cells.insert(1, html.td(module))
    casename = testnode.split('-')[1]
    url = url+testnode.split('-')[4][:-1]
    cells.insert(2, html.td(casename))
    cells.insert(3, html.td(url))
    cells.insert(5, html.td(runtime, class_='col-time'))

    cells.pop(6)
    cells.pop()

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    # report.casename = str(item.function.__code__.co_varnames) #获取方法参数名
    # report.casename = str(item.node_id)

报告如下:

 

 测试数据:

 

posted @ 2020-11-10 14:45  乐乐熊小妹  阅读(727)  评论(0)    收藏  举报