Jmeter之写入测试结果到Excel文件
在jmter测试过程中,有时候需要将测试结果写入到excel文件,最近通过柠檬班学习到相关方法,下面将将相关方法整理,方便以后使用;
jmeter写结果到excel文件,需要通过调用java方法实现,分为3步:
1)下载excel依赖包jxl.jar
下载地址:
链接:https://pan.baidu.com/s/11LMFAK4lRlh44EHECSSO2A
提取码:414q
2)编写java代码,并导出为jar包,将jar包置于jmeter安装目录下/lib/ext目录,代码如下:
注:项目中需要导入jxl.jar包
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import jxl.Cell; import jxl.LabelCell; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Colour; import jxl.format.VerticalAlignment; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; public class CWOutputFile { /** * wOutputFile写结果文件 wOutputFile(文件路径、用例编号、用例标题、预期结果、实际结果、测试结果) * @throws IOException * @throws BiffException * @throws WriteException */ public void wOutputFile(String filepath,String caseNo,String testPoint,String testData,String preResult,String fresult) throws BiffException, IOException, WriteException{ File output=new File(filepath); String result = ""; InputStream instream = new FileInputStream(filepath); Workbook readwb = Workbook.getWorkbook(instream); WritableWorkbook wbook = Workbook.createWorkbook(output, readwb); //根据文件创建一个操作对象 WritableSheet readsheet = wbook.getSheet(0); //定位到文件的第一个sheet页签 int rsRows = readsheet.getRows(); //获取sheet页签的总行数 /******************设置字体样式***************************/ WritableFont font = new WritableFont(WritableFont.createFont("宋体"),10,WritableFont.NO_BOLD); WritableCellFormat wcf = new WritableCellFormat(font); /****************************************************/ Cell cell = readsheet.getCell(0,rsRows); //获取sheet页的单元格 if(cell.getContents().equals("")){ Label labetest1 = new Label(0,rsRows,caseNo); //第一列:用例编号 Label labetest2 = new Label(1,rsRows,testPoint);//第二列:用例标题 Label labetest3 = new Label(2,rsRows,testData); //第三列:测试数据 Label labetest4 = new Label(3,rsRows,preResult);//第四列:预期结果 Label labetest5 = new Label(4,rsRows,fresult); //第五列:实际结果 if(preResult.equals(fresult)){ result = "通过"; wcf.setBackground(Colour.BRIGHT_GREEN); //预期结果和实际结果相同,测试通过 } else{ result = "不通过"; wcf.setBackground(Colour.RED); //预期结果和实际结果不相同,测试不通过 } Label labetest6 = new Label(5,rsRows,result,wcf);//第六列:测试结果 readsheet.addCell(labetest1); readsheet.addCell(labetest2); readsheet.addCell(labetest3); readsheet.addCell(labetest4); readsheet.addCell(labetest5); readsheet.addCell(labetest6); } wbook.write(); wbook.close(); } /**cOutputFile 创建输出Excel文件 cOutputFile,tradeType为文件名称前缀,返回结果:文件路径,作为wOutputFile写入结果文件的入参 * @throws IOException * @throws WriteException */ public String cOutputFile(String tradeType) throws IOException, WriteException{ String temp_str = ""; Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); temp_str = sdf.format(dt); //获取时间戳 String filepath = "D:\\\\"+tradeType+"_output_" + "_" + temp_str + ".xls"; File output = new File(filepath); if(!output.isFile()){ //文件不存在,创建新文件 output.createNewFile(); //写文件 WritableWorkbook writeBook = Workbook.createWorkbook(output); WritableSheet sheet = writeBook.createSheet("输出结果", 0); //命名sheet WritableFont headfont = new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.BOLD);//设置首行字体为宋体,11号,加粗 WritableCellFormat headwcf = new WritableCellFormat(headfont); headwcf.setBackground(Colour.GRAY_25); sheet.setColumnView(0, 11); //设置列宽 sheet.setColumnView(1, 20); sheet.setColumnView(2, 40); sheet.setColumnView(3, 10); sheet.setColumnView(4, 10); sheet.setColumnView(5, 10); headwcf.setAlignment(Alignment.CENTRE); //文字居中 headwcf.setVerticalAlignment(VerticalAlignment.CENTRE); Label labe00 = new Label(0,0,"用例编号",headwcf); //写入内容:Label(列号,行号,内容) Label labe10 = new Label(1,0,"用例标题",headwcf); Label labe20 = new Label(2,0,"测试数据",headwcf); Label labe30 = new Label(3,0,"预期结果",headwcf); Label labe40 = new Label(4,0,"实际结果",headwcf); Label labe50 = new Label(5,0,"执行结果",headwcf); sheet.addCell(labe00); sheet.addCell(labe10); sheet.addCell(labe20); sheet.addCell(labe30); sheet.addCell(labe40); sheet.addCell(labe50); writeBook.write(); writeBook.close(); } return filepath; } }
导出jar包方法:选择项目->右击鼠标->选择export ->选择JAR File
jar包(CWOutputFile.jar)下载地址:
链接:https://pan.baidu.com/s/1LsOCPMD9hUfea8j-rPy4Xg
提取码:7erg
3)jmeter通过beanshell取样器调用java方法实现创建excel文件和写入结果到Excel文件
创建Excel文件:
//新建对象 t = new CWOutputFile(); //新建excel文件 String filepath = t.cOutputFile("测试"); //保存excel文件路径保存为jmeter变量 vars.put("filepath",filepath);
保存结果到Excel文件:
//创建写回结果的对象 s = new CWOutputFile(); //参数准备 String preResult = vars.get("preResult"); //获取参数化文件中的变量,返回值为字符串类型,否则直接引用为json类型 String fresult = vars.get("fresult");//获取正则表达式中的结果文件,返回值为字符串类型,否则直接引用为json类型 //写结果到excel文件,调用方法wOutputFile方法 s.wOutputFile("${filepath}","${caseNo}","${testPoint}","${testData}",preResult,fresult);
执行结果:
注:
1)当写入文件的参数不是字符串类型,例如json格式时,需要通过vars.get(参数名)方法,将参数转化为字符串类型,然后写入结果文件;
2)写入文件中的参数如果包含中文,csv数据文件编码格式要置为空,否则会出现字符乱码;