Jmeter—生成excel结果文件
相信很多用jmeter进行接口测试的童鞋都会有这样的苦恼:同时执行上百条测试案例,如何能轻松加愉快地检查案例输出结果?仅仅靠jmeter的断言、debug sampler、察看结果树等是无法满足我们要求的!下面跟大家分享一个小技巧,利用beanshell和外部jar包来生成excel结果文件。

1、下载开源jar包
下载jxl.jar, fastjson.jar(本文以json接口为例),并放到jmeter的lib目录下。
2、开发外部jar包
(1)创建CWResultFile java项目,创建CWOutputFile类,该类包含两个方法,cOutputFile用于创建结果文件,wOutputFile用于写结果文件。
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.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;
import jxl.write.biff.RowsExceededException;
/*
*导入jxl.jar;
*后续扩充功能,sheet2增加测试报告展现;------待实现;
*/
public class CWOutputFile {
/*
* wOutputFile方法写结果文件
* wOutputFile(文件路径,案例编号,测试验证点,预期结果,实际结果,错误码,状态码,响应结果)
*/
public void wOutputFile(String filepath, String caseNo,
String testPoint, String preResult, String fresult, String errCode,
String status, String respond) throws IOException,
RowsExceededException, WriteException, BiffException {
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);
// int rsColumns = readsheet.getColumns(); //获取Sheet表中所包含的总列数
int rsRows = readsheet.getRows(); // 获取Sheet表中所包含的总行数
/********************************字体样式设置 ****************************/
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10,
WritableFont.NO_BOLD);// 字体样式
WritableCellFormat wcf = new WritableCellFormat(font);
/***********************************************************************/
Cell cell1 = readsheet.getCell(0, rsRows);
if (cell1.getContents().equals("")) {
Label labetest1 = new Label(0, rsRows, caseNo); // 第1列--案例编号;
Label labetest2 = new Label(1, rsRows, testPoint); // 第2列--验证测试点;
Label labetest3 = new Label(2, rsRows, preResult); // 第3列--预期结果;
Label labetest4 = new Label(3, rsRows, fresult); // 第4列--实际结果;
Label labetest5 = new Label(4, rsRows, errCode); // 第5列--错误码;
if (preResult == fresult) {
result = "通过";
wcf.setBackground(Colour.BRIGHT_GREEN); // 通过案例标注绿色
} else {
result = "不通过";
wcf.setBackground(Colour.RED); // 不通过案例标注红色
}
Label labetest6 = new Label(5, rsRows, result, wcf); // 第6列--执行结果;
Label labetest7 = new Label(6, rsRows, status); // 第7列--状态码
Label labetest8 = new Label(7, rsRows, respond); // 第8列--响应结果
readsheet.addCell(labetest1);
readsheet.addCell(labetest2);
readsheet.addCell(labetest3);
readsheet.addCell(labetest4);
readsheet.addCell(labetest5);
readsheet.addCell(labetest6);
readsheet.addCell(labetest7);
readsheet.addCell(labetest8);
}
wbook.write();
wbook.close();
}
/*
* cOutputFile方法创建输出文件,传入参数为交易类型,如开户等;
* cOutputFile方法返回文件路径,作为wOutputFile的入参;
*/
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); // 获取时间戳
// 相对路径默认为 apache-jmeter-3.1\bin
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); // createSheet(sheet名称,第几个sheet)
WritableFont headfont = new WritableFont(
WritableFont.createFont("宋体"), 11, WritableFont.BOLD); // 字体样式
WritableCellFormat headwcf = new WritableCellFormat(headfont);
headwcf.setBackground(Colour.GRAY_25); // 灰色颜色
Sheet.setColumnView(0, 11); // 设置列宽度setColumnView(列号,宽度)
Sheet.setColumnView(1, 30);
Sheet.setColumnView(2, 35);
Sheet.setColumnView(3, 35);
Sheet.setColumnView(4, 18);
Sheet.setColumnView(5, 11);
Sheet.setColumnView(6, 11);
Sheet.setColumnView(7, 50);
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);
Label labe60 = new Label(6, 0, "返回状态", headwcf);
Label labe70 = new Label(7, 0, "响应结果", headwcf);
Sheet.addCell(labe00);
Sheet.addCell(labe10);
Sheet.addCell(labe20);
Sheet.addCell(labe30);
Sheet.addCell(labe40);
Sheet.addCell(labe50);
Sheet.addCell(labe60);
Sheet.addCell(labe70);
writeBook.write();
writeBook.close();
}
return filepath;
}
}
 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.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;import jxl.write.biff.RowsExceededException;/**导入jxl.jar;*后续扩充功能,sheet2增加测试报告展现;------待实现;*/public class CWOutputFile {/** wOutputFile方法写结果文件* wOutputFile(文件路径,案例编号,测试验证点,预期结果,实际结果,错误码,状态码,响应结果)*/public  void wOutputFile(String filepath, String caseNo,String testPoint, String preResult, String fresult, String errCode,String status, String respond) throws IOException,RowsExceededException, WriteException, BiffException {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);// int rsColumns = readsheet.getColumns(); //获取Sheet表中所包含的总列数int rsRows = readsheet.getRows(); // 获取Sheet表中所包含的总行数/********************************字体样式设置 ****************************/WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10,WritableFont.NO_BOLD);// 字体样式WritableCellFormat wcf = new WritableCellFormat(font);/***********************************************************************/Cell cell1 = readsheet.getCell(0, rsRows);if (cell1.getContents().equals("")) {Label labetest1 = new Label(0, rsRows, caseNo);    // 第1列--案例编号;Label labetest2 = new Label(1, rsRows, testPoint); // 第2列--验证测试点;Label labetest3 = new Label(2, rsRows, preResult); // 第3列--预期结果;Label labetest4 = new Label(3, rsRows, fresult);   // 第4列--实际结果;Label labetest5 = new Label(4, rsRows, errCode);   // 第5列--错误码;if (preResult == fresult) {result = "通过";wcf.setBackground(Colour.BRIGHT_GREEN); // 通过案例标注绿色} else {result = "不通过";wcf.setBackground(Colour.RED);          // 不通过案例标注红色}Label labetest6 = new Label(5, rsRows, result, wcf); // 第6列--执行结果;Label labetest7 = new Label(6, rsRows, status);      // 第7列--状态码Label labetest8 = new Label(7, rsRows, respond);     // 第8列--响应结果readsheet.addCell(labetest1);readsheet.addCell(labetest2);readsheet.addCell(labetest3);readsheet.addCell(labetest4);readsheet.addCell(labetest5);readsheet.addCell(labetest6);readsheet.addCell(labetest7);readsheet.addCell(labetest8);}wbook.write();wbook.close();}/** cOutputFile方法创建输出文件,传入参数为交易类型,如开户等;* cOutputFile方法返回文件路径,作为wOutputFile的入参;*/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); // 获取时间戳// 相对路径默认为 apache-jmeter-3.1\binString 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); // createSheet(sheet名称,第几个sheet)WritableFont headfont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.BOLD); // 字体样式WritableCellFormat headwcf = new WritableCellFormat(headfont);headwcf.setBackground(Colour.GRAY_25); // 灰色颜色Sheet.setColumnView(0, 11); // 设置列宽度setColumnView(列号,宽度)Sheet.setColumnView(1, 30);Sheet.setColumnView(2, 35);Sheet.setColumnView(3, 35);Sheet.setColumnView(4, 18);Sheet.setColumnView(5, 11);Sheet.setColumnView(6, 11);Sheet.setColumnView(7, 50);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);Label labe60 = new Label(6, 0, "返回状态", headwcf);Label labe70 = new Label(7, 0, "响应结果", headwcf);Sheet.addCell(labe00);Sheet.addCell(labe10);Sheet.addCell(labe20);Sheet.addCell(labe30);Sheet.addCell(labe40);Sheet.addCell(labe50);Sheet.addCell(labe60);Sheet.addCell(labe70);writeBook.write();writeBook.close();}return filepath;}}
(2)导出CWResultFile.jar包,并放到jmeter的lib/ext目录下。
3、脚本示例
(1)准备案例数据

(2)调用cOutputFile方法创建结果文件

(3)调用wOutputFile方法写结果文件

(4)生成结果文件


4、下一阶段展望
持续集成;
作者:Tomandy
链接:https://www.jianshu.com/p/eebbeaf80fd2
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Jmeter—生成excel结果文件的更多相关文章
- PHP生成excel表格文件并下载
		
本文引自网络,仅供自己学习之用. 利用php导出excel我们大多会直接生成.xls文件,这种方便快捷. function createtable($list,$filename){ header(& ...
 - java生成excel报表文件
		
此次简单的操作将数据从数据库导出生成excel报表以及将excel数据导入数据库 首先建立数据库的连接池: package jdbc; import java.io.FileInputStream; ...
 - 生成Excel.xlsx文件 iOS
		
使用到的三方库 https://github.com/jmcnamara/libxlsxwriter cocoapods导入 pod 'libxlsxwriter', '~> 0.8.3' 1. ...
 - springboot2.1.8使用poi导出数据生成excel(.xlsx)文件
		
前言:在实际开发中经常需要将数据库的数据导出成excel文件,poi方式则是其中一种较为常用的导出框架.简单读取excel文件在之前的一篇有说明 本项目实现需求:user发出一个导出student信息 ...
 - 根据xlsx模板生成excel数据文件发送邮件代码
		
package mail; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
 - unity 读取excel表 生成asset资源文件
		
做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...
 - 如何生成excel文件作为图像识别结果
		
如何生成excel文件作为图像识别结果 在进行大规模图像处理的时候,如果能够以表格的形式生成结果文件,将非常的直观.这个时候,选择excel作为结果输出文件,将是合适的. 查询相关资料,有很多关于ex ...
 - [转]powerDesigner生成excel版本的数据库文件
		
powerDesigner生成excel版本的数据库文件 出处:http://ray-allen.iteye.com/blog/1893347 脚本 excel 今天收到一个需求,要把数据库设计给一 ...
 - php生成excel文件的简单方法
		
生成excel文件,最简单的莫过于把数据库的数据导入到excel就行了. 生成excel 当然使用的是 phpExcel http://www.jbxue.com/tags/phpexcel.html ...
 
随机推荐
- <J2EE学习笔记>关于Servlet的讲义
			
题外话:接触java又是半年之前的事情了,当初好好学了java却把cpp给忘了,到现在又把手里发热的cpp给放下重新捡起来java,究竟这两种OOP语言我能不能清晰分开记住呢 以下全部课件来自于同济大 ...
 - POJ2278 DNA Sequence —— AC自动机 + 矩阵优化
			
题目链接:https://vjudge.net/problem/POJ-2778 DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Tota ...
 - WCF异常处理
			
[读书笔记] 在进行分布式应用的异常处理时需要解决和考虑的基本要素: 异常的封装:服务端抛出的异常如何序列化传递到客户端 敏感信息的屏蔽:抛出的异常往往包含一些敏感的信息,直接将服务操作执行过程抛出的 ...
 - LNMP搭建随笔
			
LNMP(即Linux+Nginx+MYSQL+PHP)是目前非常热门的动态网站部署架构,一般是指: Linux:如RHEL.Centos.Debian.Fedora.Ubuntu等系统. Nginx ...
 - POJ-3680:Intervals (费用流)
			
You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task i ...
 - Qt之log数据展示模块简要实现
			
Log模块主要用于实时测井数据的显示和测后曲线数据的预览和打印,为更好的展示对Qt中相关知识点的应用,特以Log模块为例对其进行简要实现. 内容导图: 一.功能需求 1.界面效果图 Log模块实现曲线 ...
 - ACM学习历程—HDU 5289 Assignment(线段树 || RMQ || 单调队列)
			
Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fro ...
 - 洛谷P1220关路灯——区间DP
			
题目:https://www.luogu.org/problemnew/show/P1220 区间DP. 代码如下: #include<iostream> #include<cstd ...
 - HDU3974(dfs+线段树)
			
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
 - InformationSecurity:template
			
ylbtech-InformationSecurity: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:ht ...