别的不说,直接上代码
主要逻辑:
通过jmeter 运行后生成的jtl(output=xml)格式的结果,通过python脚本转html报告。(output=csv格式的jmeter -o 自带的可以生成)
准备python3环境+python+allure
vim JtlToReportHTML.py
# -*- coding: utf-8 -*-
# @Time : 2022/4/11 下午6:06
# @Author : sleeli
# @File : JtlToReportHTML.py
# @Software: PyCharm
# -*- coding: utf-8 -*-
# @Time : 2022/4/11 4:29 下午
import xml.etree.cElementTree as ET
import json, os, uuid
import sys
# xmlObject xml工程
# checkString 这个其实是固定值 httpSample,只不过后面想要获取其他结构参数化
# num 当时想嵌套多层,后来发现allure只三层结构,其实这个也没啥用
# result 传递解析的xml
# demoFile 生成的pytes文件
# featureIndex 用来排序参数用例,按照jmeter中的树结果
# storyIndex 同上,是第二层级的排序
def checkChildren(xmlObject, checkString, num, result, demoFile, featureIndex, storyIndex):
for children in xmlObject:
try:
if num == 1 and children.attrib['sby'] != "0":
featureIndexStr = '#' + str(featureIndex) + " " if featureIndex >= 10 else '#0' + str(
featureIndex) + " "
result["feature"] = featureIndexStr + children.attrib['lb']
featureIndex += 1
if num >= 2 and children.tag == "sample":
storyIndexStr = '#' + str(storyIndex) + " " if storyIndex >= 10 else '#0' + str(
storyIndex) + " "
result["story"] = storyIndexStr + children.attrib['lb']
storyIndex += 1
except:
pass
if children.tag == checkString:
result['case_name'] = children.attrib['lb']
for httpSampleChildren in children:
result[httpSampleChildren.tag] = httpSampleChildren.text
if httpSampleChildren.tag == 'assertionResult':
for assertionResultChildren in httpSampleChildren:
result[assertionResultChildren.tag] = assertionResultChildren.text
feature = result['feature'] if "feature" in result else None
story = result['story'] if 'story' in result else None
case_name = result['case_name'] if 'case_name' in result else None
URL = result['java.net.URL'] if 'java.net.URL' in result else None
method = result['method'] if 'method' in result else None
requestHeader = result['requestHeader'] if 'requestHeader' in result else None
queryString = result['queryString'] if 'queryString' in result else None
responseData = result['responseData'] if 'responseData' in result else None
failureMessage = result['failureMessage'] if 'failureMessage' in result else None
failure = result['failure'] if 'failure' in result else "false"
print(feature, story, case_name, failureMessage, URL)
storyString = "@allure.story('" + result['story'] + "') # 二级目录" if 'story' in result else ''
pyString = '''
@allure.feature('{feature}') # 一级目录{story}
@allure.title("{case_name}")
def test_allure_report_{num}():
with allure.step('请求url:{URL}'):
print('请求url:{URL}')
with allure.step('请求方法:{method}' ):
print('请求方法:{method}' )
with allure.step('请求头:{requestHeader}' ):
print('请求头:{requestHeader}' )
with allure.step(\'''请求数据:{queryString}\'''):
print(\'''请求数据:{queryString}\''')
with allure.step(\'''接口返回:{responseData}\'''):
print(\'''接口返回:{responseData}\''')
with allure.step(\'''断言结果:{failureMessage}\'''):
print(\'''断言结果:{failureMessage}\''')
assert "{failure}" == 'false' '''.format(feature=feature, story=storyString, case_name=case_name,
num=str(uuid.uuid1()).replace('-', ''),
URL=URL, method=method,
requestHeader=str(requestHeader).replace('\n', '').replace('\r', ''),
queryString=str(json.dumps(queryString)).replace("'", "\""),
responseData=str(json.dumps(responseData)).replace("'", "\""),
failureMessage=str(failureMessage).replace("'", "\""), failure=failure)
print(pyString)
with open(demoFile, 'a') as c:
c.write(pyString)
else:
checkChildren(children, checkString, num + 1, result, demoFile, featureIndex, storyIndex)
if __name__ == '__main__':
# 通过命令行来执行 ,例如python3 JtlToReportHTML.py test.jtl test_demo.py report
commonndLines = sys.argv
print(commonndLines)
with open(commonndLines[2], "w") as demo:
demo.write('''
# -*- coding: utf-8 -*-
import allure
''')
report_resource = commonndLines[3]+"/resource"
report_html = commonndLines[3] + "/html"
tree = ET.parse(commonndLines[1])
root = tree.getroot()
checkChildren(root, "httpSample", 1, {}, commonndLines[2], 1, 1)
pyString = 'pytest --capture=no ' + commonndLines[2] + ' --alluredir ' + report_resource
os.system(pyString)
alSring = "allure generate " + report_resource + " -o " + report_html + " --clean"
os.system(alSring)
通过命令行来执行 ,例如
# 会自动创建报告目录report2
python3 JtlToReportHTML.py test.jtl test_demo.py report2
报告目录下有有资源和文档,如下(我是在report2目录下生成的报告)
然后启动谷歌浏览器的本地模式(我的mac,其他系统的自己百度吧,关键字搜索浏览器本地跨域 )
open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=MyChromeDevUserData
最后将report/html/index.html拖到浏览器打开查看效果:(注意浏览器需要完全退出后在开本地模式,否则不生效)