背景
在做单元测试的过程中,经常需要新建测试类,方便我们对接口进行单元测试。但这个手动的创建过程,相对比较缓慢,有些通用的地方是否可以一键生成。下面就介绍一种自动生成测试类的方法,而且可以根据实际需求,生成定制化的测试类。
一、自动生成Excel文件
该excel主要用于存放接口测试用例的模版,对应一个接口方法一个sheet,如下图
对应生成此Excel的代码
genExcelFile方法
public static void genExcelFile(Class<?> clazz) {
//获取类名
String excelFileName = clazz.getSimpleName();
File excelFileDir = new File(ServiceTemplate.EXCEL_FILE_PATH);
//判断Excel目录是否存在
if(!excelFileDir.exists())
excelFileDir.mkdirs();
File excelFile = new File(excelFileDir,excelFileName+".xlsx");
Method[] methods = clazz.getMethods();
XSSFWorkbook workbook = new XSSFWorkbook();
for (Method method : methods){
Class<?>[] parameterTypes = method.getParameterTypes();
String paramJson = "";
try {
paramJson = new ObjectMapper().writeValueAsString(parameterTypes[0].newInstance());
}catch (Exception e){
paramJson = "{}";
}
XSSFSheet sheet = workbook.createSheet(method.getName());
sheet.setDefaultColumnWidth(15);
//**预留,可根据自己需求定制
writeCell(sheet,0,0,"caseName",true);
writeCell(sheet,0,1,"caseDescription",false);
writeCell(sheet,0,2,"caseType",false);
writeCell(sheet,0,3,"casePriority",false);
writeCell(sheet,0,4,"caseRun",false);
writeCell(sheet,0,5,"paramJson",false);
writeCell(sheet,0,6,"url",false);
writeCell(sheet,1,0,"用例名称",true);
writeCell(sheet,1,1,"用例描述",false);
writeCell(sheet,1,2,"正常流程",false);
writeCell(sheet,1,3,"P0",false);
writeCell(sheet,1,4,"Y",false);
writeCell(sheet,1,5,paramJson,false);
writeCell(sheet,1,6,"/",false);
}
FileOutputStream fo;
try {
fo = new FileOutputStream(excelFile);
workbook.write(fo);
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
writeCell方法
/**
* 写入数据到表格
* @param sheet
* @param rowNum
* @param cellNum
* @param value
* @param isCreate
*/
private static void writeCell(XSSFSheet sheet, int rowNum, int cellNum,String value,boolean... isCreate){
XSSFRow row = null;
if(isCreate.length == 0 || isCreate[0]){
row = sheet.createRow(rowNum);
}else{
row = sheet.getRow(rowNum);
}
XSSFCell cell = row.createCell(cellNum);
cell.setCellValue(value);
}
二、生成测试类文件
1.genTestFile方法
代码如下(示例):
/**
* 生成测试入口文件
* @param clazz
*/
private static void genTestFile(Class<?> clazz){
String testFileName = "Test"+clazz.getSimpleName();
File testFileDir = new File(TestTemplate.TEST_FILE_PATH);
String serviceFileName = "Service"+clazz.getSimpleName();
//判断Excel目录是否存在
if(!testFileDir.exists())
testFileDir.mkdirs();
File testFile = new File(testFileDir,testFileName+".java");
StringBuilder sb = new StringBuilder(TestTemplate.test_template);
//生成测试类
for (Method method : clazz.getMethods()){
String methodString = TestTemplate.method_template;
methodString = methodString.replaceAll("\\$methodName",method.getName());
methodString = methodString.replaceAll("\\$serviceName",serviceFileName);
sb.insert(sb.indexOf("//method"),methodString);
}
try {
String writeStr = sb.toString();
writeStr = writeStr.replaceAll("\\$serviceName",serviceFileName);
writeStr = writeStr.replaceAll("\\$testFileName",testFileName);
writeStr = writeStr.replaceAll("\\$excelFileName",clazz.getSimpleName());
BufferedWriter bw = new BufferedWriter(new FileWriter(testFile));
bw.write(writeStr);
bw.flush();
bw.close();
}catch (IOException e){
e.printStackTrace();
}
}
2.生成的测试类示例
根据自身项目定制生成的测试类(此示例根据自身需求定制),
生成的@DataDriver,就是获取excel中的接口测试用例数据,
总结
写这些方法的目的,就是为了方便大家更加快速的做单元测试,提升效率。如果有更好的建议,欢迎大家来一起交流探讨。
喜欢的朋友动动发财的小手,帮忙点个赞