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 ...
随机推荐
- utf8_general_ci和utf8_unicode_ci的比较
看到很多数据库的设计对于中文字符都是选择选用utf8_general_ci而非utf8_unicode_ci utf8_general_ci和utf8_unicode_ci的区别并不大:utf8_un ...
- Win7、Win8、Win10始终以管理员身份运行程序。
在Win7.Win8.Win10系统中,以管理员身份运行程序很麻烦,一般有以下几种方式: 1.在可执行程序或快捷方式上右键,以管理员身份运行: 2.在可执行程序或快捷方式上右键->属性-> ...
- 分享知识-快乐自己:SpringMvc中 页面日期格式到后台的类型转换
日期格式的类型转换: 以往在 from 表单提交的时候,都会有字符串.数字.还有时间格式等信息. 往往如果是数字提交的话底层会自动帮我们把类型进行了隐式转换. 但是日期格式的却不能自动转换,这就需要我 ...
- codeforces 659B B. Qualifying Contest(水题+sort)
题目链接: B. Qualifying Contest time limit per test 1 second memory limit per test 256 megabytes input s ...
- Python: scikit-image binary descriptor
这个用例说明 BRIEF binary description algorithm from skimage import data from skimage import transform as ...
- 【Lintcode】105.Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- MUI框架开发HTML5手机APP
出处:http://www.cnblogs.com/jerehedu/p/7832808.html 前 言 JRedu 随着HTML5的不断发展,移动开发成为主流趋势!越来越多的公司开始选择使用H ...
- Eclipse插件:tomcatPluginV33.zip 安装
一.下载 地址:http://www.eclipsetotale.com/tomcatPlugin.html 二.安装 1.解压tomcatPluginV33.zip到Eclipse的\feature ...
- Ubuntu下如何禁用IPv6
Ubuntu下如何禁用IPv6 2013-10-16 11:32:02 分类: HADOOP 分布式下的hadoop/hbase运行总出问题,zookeeper连接总是出问题,怀疑可能是ip ...
- (六)编写基类BaseDao
在action中继承了ActionSupport和其它一些公共属性,如selectedRow等:可能以后还会产生更多公共的内容,所以应该把这些共有的抽取出来,放入到一个基本action中,我们命名为B ...