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 @   乐乐熊小妹  阅读(698)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2018-11-10 python连接redis,redis集群
点击右上角即可分享
微信分享提示