- Excel测试记录文件模板
设计Excel的数据结构
- 下载jxl.jar
- Java 代码编码
通过将java代码打包成jar,然后放入Jmeter目录下进行引用
代码如下:
package com.sishi;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.*;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author sishi
* @createdate 2022-02-20
* @// TODO: 将Jmeter的线程组测试结果写入excel
*/
public class JmeterResult {
/**
* 创建xls文件,并返回该文件路径
*
* @param fileName 文件路径与文件名,不需要后缀, 如:D:\\测试\\登录测试结果
* @return filePath 返回所创建文件的文件路径
* @throws Exception
*/
public String CreateResultFile(String fileName){
// 1.根据文件名创建文件
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss");
Date datetime = new Date();
String folderPath = fileName.substring(0, fileName.lastIndexOf("\\"));//截取目录路径
WritableWorkbook wb = null;
String filePath = "";
try{
String timestamp = sdf.format(datetime);
File initialPath = new File(folderPath);
//如果该文件夹不存在,则先创建
if (!initialPath.exists()) {
// System.out.println("创建目录");
initialPath.mkdirs();
}
filePath = fileName + "_Test_result" + "_" + timestamp + ".xls";//设置文件名
// 1. 新建文件
File output = new File(filePath);
if (!output.isFile()) {
output.createNewFile();
wb = Workbook.createWorkbook(new File(filePath));
// 2. 新建数据表,表名与页数
WritableSheet ws = wb.createSheet("test_result", 0);
// 3. 设置样式
WritableFont headFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.BOLD);
WritableCellFormat headCellFormat = new WritableCellFormat(headFont);
headCellFormat.setBackground(Colour.GRAY_25);
// 4.写入单元格(列,行,值,样式)
Label caseNo = new Label(0, 0, "用例编号",headCellFormat);
Label caseName = new Label(1, 0, "用例名称",headCellFormat);
Label testData = new Label(2, 0, "测试数据",headCellFormat);
Label expectedResult = new Label(3, 0, "预期结果",headCellFormat);
Label actualResult = new Label(4, 0, "实际结果",headCellFormat);
Label testResult = new Label(5, 0, "测试结果",headCellFormat);
// 5.写入sheet
ws.addCell(caseNo);
ws.addCell(caseName);
ws.addCell(testData);
ws.addCell(expectedResult);
ws.addCell(actualResult);
ws.addCell(testResult);
// 6. 写入workbook
wb.write();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 7.关闭
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
// 8. 返回所生成文件的路径
return filePath;
}
/**
* 写测试数据与测试结果到Excel
* @param filePath Excel文件路径
* @param caseNo 用例编号
* @param caseName 用例名称
* @param testData 测试数据
* @param expectedResult 期望结果
* @param actualResult 实际结果
* @throws Exception
*/
public void GenerateTestResult(String filePath, String caseNo, String caseName, String testData,String expectedResult,
String actualResult){
WritableWorkbook writeWb = null;
String testResult = "";
try{
File outPut = new File(filePath);
//创建只读对象的副本
Workbook wb = Workbook.getWorkbook(outPut);
//创建真实写入对象
writeWb = Workbook.createWorkbook(outPut, wb);
//获取第一个工作表
WritableSheet sheet = writeWb.getSheet(0);
int rowNum = sheet.getRows();//获取行数,用作下一次结果写入的起始行
//设置test单元格的样式
WritableFont headFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.BOLD);
WritableCellFormat testResultCellFormat = new WritableCellFormat(headFont);
//写入单元格数据
Label caseNoValue = new Label(0,rowNum,caseNo);
Label caseNameValue = new Label(1,rowNum,caseName);
Label testDataValue = new Label(2,rowNum,testData);
Label expectedResultValue = new Label(3,rowNum,expectedResult);
Label actualResultValue = new Label(4,rowNum,actualResult);
//判断expectedResult是否等于actualResult,并得到testResult
if(expectedResult.equals(actualResult)){
testResult = "通过";
testResultCellFormat.setBackground(Colour.GREEN);
}else{
testResult = "失败";
testResultCellFormat.setBackground(Colour.RED);
}
Label testResultValue = new Label(5,rowNum,testResult,testResultCellFormat);
sheet.addCell(caseNoValue);
sheet.addCell(caseNameValue);
sheet.addCell(testDataValue);
sheet.addCell(expectedResultValue);
sheet.addCell(actualResultValue);
sheet.addCell(testResultValue);
writeWb.write();
}catch (Exception e){
e.printStackTrace();
}finally{
try {
writeWb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
// 测试 CreateResultFile
public static void testCreateResultFile() {
JmeterResult rs = new JmeterResult();
String path = null;
try {
path = rs.CreateResultFile("D:\\TestResults\\登录\\登录测试结果");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(path);
}
//测试 GenerateTestResult
public static void testGenerateTestResult() throws Exception {
JmeterResult js = new JmeterResult();
String filePath = js.CreateResultFile("D:\\测试Main\\测试");
js.GenerateTestResult(filePath,"login_001","登录成功","user:001 pad=0","{\"result\":\"success\"}",
"{\"result:\":\"success\"}");
js.GenerateTestResult(filePath,"2","demo测试02","user:002 pad=0","{\"result:\":\"success\"}",
"{\"result:\":\"failed\"}");
}
public static void main(String[] args) throws Exception {
JmeterResult.testGenerateTestResult();
}
}
-
打包成jar包(基于Idea,网上有很多优秀博客供参考,这里只做简单记录)
4.1 打开File->Project Structure…->Artifacts
4.2 重新Build,,项目与Artifacts都重新编译。如果之后有代码更新,也需要重新编译
4.3 到4.1的输出目录中找到所生成的jar包,并将其拷贝到Jmeter的lib\ext目录下。 再将jxl.jar也拷贝到该目录下(好像是我没打包好)
-
测试
新建一个BeanShell 取样器, script中写入如下代码
-
运行结果(HTTP请求失败是因为服务器没运行,不是这里的重点)
总结
这里编写java代码,并将其打包成jar放入Jmeter中进行调用,成功创建了一个Excel文件,返回该文件的路径。
下一篇将运行测试用例,并将结果写入该excel文件。
谢谢!