? 相信很多用jmeter進(jìn)行接口測(cè)試的童鞋都會(huì)有這樣的苦惱:同時(shí)執(zhí)行上百條測(cè)試案例,如何能輕松加愉快地檢查案例輸出結(jié)果??jī)H僅靠jmeter的斷言、debug sampler、察看結(jié)果樹(shù)等是無(wú)法滿足我們要求的!下面跟大家分享一個(gè)小技巧,利用beanshell和外部jar包來(lái)生成excel結(jié)果文件。

? 1、下載開(kāi)源jar包
下載jxl.jar, fastjson.jar(本文以json接口為例),并放到j(luò)meter的lib目錄下。
? 2、開(kāi)發(fā)外部jar包
(1)創(chuàng)建CWResultFile java項(xiàng)目,創(chuàng)建CWOutputFile類(lèi),該類(lèi)包含兩個(gè)方法,cOutputFile用于創(chuàng)建結(jié)果文件,wOutputFile用于寫(xiě)結(jié)果文件。
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;
/*
*導(dǎo)入jxl.jar;
*后續(xù)擴(kuò)充功能,sheet2增加測(cè)試報(bào)告展現(xiàn);------待實(shí)現(xiàn);
*/
public class CWOutputFile {
/*
* wOutputFile方法寫(xiě)結(jié)果文件
* wOutputFile(文件路徑,案例編號(hào),測(cè)試驗(yàn)證點(diǎn),預(yù)期結(jié)果,實(shí)際結(jié)果,錯(cuò)誤碼,狀態(tài)碼,響應(yīng)結(jié)果)
*/
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); // 根據(jù)文件創(chuàng)建一個(gè)操作對(duì)象
WritableSheet readsheet = wbook.getSheet(0);
// int rsColumns = readsheet.getColumns(); //獲取Sheet表中所包含的總列數(shù)
int rsRows = readsheet.getRows(); // 獲取Sheet表中所包含的總行數(shù)
/********************************字體樣式設(shè)置 ****************************/
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列--案例編號(hào);
Label labetest2 = new Label(1, rsRows, testPoint); // 第2列--驗(yàn)證測(cè)試點(diǎn);
Label labetest3 = new Label(2, rsRows, preResult); // 第3列--預(yù)期結(jié)果;
Label labetest4 = new Label(3, rsRows, fresult);? // 第4列--實(shí)際結(jié)果;
Label labetest5 = new Label(4, rsRows, errCode);? // 第5列--錯(cuò)誤碼;
if (preResult == fresult) {
result = "通過(guò)";
wcf.setBackground(Colour.BRIGHT_GREEN); // 通過(guò)案例標(biāo)注綠色
} else {
result = "不通過(guò)";
wcf.setBackground(Colour.RED);? ? ? ? ? // 不通過(guò)案例標(biāo)注紅色
}
Label labetest6 = new Label(5, rsRows, result, wcf); // 第6列--執(zhí)行結(jié)果;
Label labetest7 = new Label(6, rsRows, status);? ? ? // 第7列--狀態(tài)碼
Label labetest8 = new Label(7, rsRows, respond);? ? // 第8列--響應(yīng)結(jié)果
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方法創(chuàng)建輸出文件,傳入?yún)?shù)為交易類(lèi)型,如開(kāi)戶(hù)等;
* cOutputFile方法返回文件路徑,作為wOutputFile的入?yún)ⅲ?/p>
*/
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); // 獲取時(shí)間戳
// 相對(duì)路徑默認(rèn)為 apache-jmeter-3.1\bin
String filepath = "D:\\\\"+tradeType+"_output_" + "_" + temp_str + ".xls"; // 以時(shí)間戳命名結(jié)果文件,確保唯一
File output = new File(filepath);
if (!output.isFile()) {
output.createNewFile(); // 如果指定文件不存在,則新建該文件
WritableWorkbook writeBook = Workbook.createWorkbook(output);
WritableSheet Sheet = writeBook.createSheet("輸出結(jié)果", 0); // createSheet(sheet名稱(chēng),第幾個(gè)sheet)
WritableFont headfont = new WritableFont(
WritableFont.createFont("宋體"), 11, WritableFont.BOLD); // 字體樣式
WritableCellFormat headwcf = new WritableCellFormat(headfont);
headwcf.setBackground(Colour.GRAY_25); // 灰色顏色
Sheet.setColumnView(0, 11); // 設(shè)置列寬度setColumnView(列號(hào),寬度)
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); // 設(shè)置文字居中對(duì)齊方式;
headwcf.setVerticalAlignment(VerticalAlignment.CENTRE); // 設(shè)置垂直居中;
Label labe00 = new Label(0, 0, "案例編號(hào)", headwcf); // Label(列號(hào),行號(hào), 內(nèi)容)
Label labe10 = new Label(1, 0, "驗(yàn)證測(cè)試點(diǎn)", headwcf);
Label labe20 = new Label(2, 0, "預(yù)期結(jié)果", headwcf);
Label labe30 = new Label(3, 0, "實(shí)際結(jié)果", headwcf);
Label labe40 = new Label(4, 0, "錯(cuò)誤碼", headwcf);
Label labe50 = new Label(5, 0, "執(zhí)行結(jié)果", headwcf);
Label labe60 = new Label(6, 0, "返回狀態(tài)", headwcf);
Label labe70 = new Label(7, 0, "響應(yīng)結(jié)果", 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)導(dǎo)出CWResultFile.jar包,并放到j(luò)meter的lib/ext目錄下。
? 3、腳本示例
(1)準(zhǔn)備案例數(shù)據(jù)

(2)調(diào)用cOutputFile方法創(chuàng)建結(jié)果文件

(3)調(diào)用wOutputFile方法寫(xiě)結(jié)果文件

(4)生成結(jié)果文件


? 4、下一階段展望
持續(xù)集成;