别的不说,直接上代码
重要逻辑:
通过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 ETimport json, os, uuidimport 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)通过命令行来实行 ,比方
# 会自动创建陈诉目次report2python3 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拖到欣赏器打开检察结果:(留意欣赏器必要完全退出后在开本地模式,否则不收效)
|