Jmeter排憂解難—生成excel結(jié)果文件

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

Jmeter接口自動(dòng)化腳本編寫(xiě)流程

? 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ù)

testcase.csv

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

ps:此處使用“僅一次控制器”是因?yàn)閷?duì)于testcase.csv的N條案例數(shù)據(jù)我們只需一個(gè)結(jié)果文件即可;

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

調(diào)用wOutputFile方法寫(xiě)文件

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

以時(shí)間戳命名輸出結(jié)果文件,確保唯一性
輸出結(jié)果文件內(nèi)容如上所示

? 4、下一階段展望

持續(xù)集成;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,551評(píng)論 19 139
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,325評(píng)論 0 17
  • ClientFile.java ServerFile.java SocketFileJFrame.java
    凱哥學(xué)堂閱讀 611評(píng)論 0 4
  • package wrapFunc; import java.io.File; import java.io.Fil...
    yoyoswj閱讀 1,043評(píng)論 0 0
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,673評(píng)論 18 399

友情鏈接更多精彩內(nèi)容