关于如何自定义修改pytest-html报告深度学习总结
第一、pytest-html执行命令总结:
pytest test_case.py --html=report.html --self-contained-html 直接html独立显示
pytest test_case.py --html=report.html 包含assets样式文件,htmL显示需要依赖于此文件
第二、关于如何修改pytest-html的报告内容:
1、如何修改report.html中的Environment部分,可在conftest.py中实现:
def pytest_configure(config): config._metadata['测试地址'] = '192.168.50.XXX'
报告结果变化:
2、如何修改report.html中的Summary部分,可在conftest.py中实现:
import pytest from py._xmlgen import html @pytest.mark.optionalhook def pytest_html_results_summary(prefix, summary, postfix): prefix.extend([html.p("测试人: 龙雄")])
报告结果变化:
3、如何修改report.html中的results-table部分,可在conftest.py中实现:
from datetime import datetime import pytest @pytest.mark.optionalhook def pytest_html_results_table_header(cells): cells.insert(2, html.th('Description')) cells.insert(3, html.th('Time', class_='sortable time', col='time')) # cells.insert(1,html.th("Test_nodeid")) cells.pop() @pytest.mark.optionalhook def pytest_html_results_table_row(report, cells): cells.insert(2, html.td(report.description)) cells.insert(3, html.td(datetime.utcnow(), class_='col-time')) # cells.insert(1,html.td(report.nodeid)) cells.pop() @pytest.mark.hookwrapper def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() report.description = str(item.function.__doc__) report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape") #设置编码显示中文
报告结果变化:
注意:如果测试用例中,传递的@pytest.mark.parametrize中的参数是列表中带字典的方式,那么报告显示的结果将为:
若传递的@pytest.mark.parametrize中的参数是列表中带元祖或列表的方式(如: bdata = [(1, "升级专业版本"),(2, "官方网站")];bdata=[[1,2],[3,4]]),那么报告显示的结果将为:
所以本人之前一直不知道如何将我的这些测试数据展示到report table中,因为本人之前一直用的是字典的方式,今天发现原来传递的数据类型会影响报告的展示内容,不知是否还有其他更好的解决方式,
有的话请大家赐教
所以本人的解决方案是,将传递的列表数组存储为元祖的方式:
import pytest from dotest import mytest import json import logging import requests bdata = mytest() s = requests.session() logging.getLogger().setLevel(logging.INFO) lb = [] for i in bdata: print(i) print(i['url']) yz = (i,i['url'],i['casename']) lb.append(yz) @pytest.mark.parametrize("bdata,f,m", lb) def testsingle(bdata,f,m): '''登录模块''' ip = bdata['ip'] url = bdata['url'] headers = json.loads(bdata['headers']) checkpoint = bdata['checkpoint'] method = bdata['method'] casename = bdata['casename'] if bdata['datatype']=='json': body = json.loads(bdata['body']) result = s.request(method=method,url=ip+url,headers=headers,json=body) r = json.loads(result.text) assert checkpoint in r['message'] logging.info("pass "+casename +" "+url)
更多参考文章推荐:
https://www.cnblogs.com/linuxchao/p/linuxchao-pytest-html.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .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语句:使用策略模式优化代码结构