测试集中报告可以看成是一个总表,这时可以用Excel 的图表工具自动生成图表,整个测试集测试通过后再统计各个级别的报告
图表模板如下
![](https://img.haomeiwen.com/i2286822/683f84044430f93d.png)
public static void statistical() throws IOException{
String TestReport=System.getProperty("user.dir")+"\\测试报告\\测试报告.xlsx";
System.out.println("报告文件"+TestReport);
FileInputStream ReportFile = new FileInputStream(TestReport);
//创建工作本
XSSFWorkbook ReportBook = new XSSFWorkbook(ReportFile);
//获取Sheet 名称
XSSFSheet TestStepSheet=ReportBook.getSheet("TestStep");
for (int i=1;i<=TestStepSheet.getLastRowNum();i++){
String txt=Excel.GetString(TestStepSheet, i,9 );
if (txt.equals("PASS")){
Constants.TestStepPass=Constants.TestStepPass+1;
}
else {
Constants.TestStepFail=Constants.TestStepFail+1;
}
}
XSSFSheet TestCaseSheet=ReportBook.getSheet("TestCase");
for (int i=1;i<=TestCaseSheet.getLastRowNum();i++){
String txt=Excel.GetString(TestCaseSheet, i,2 );
if (txt.equals("PASS")){
Constants.TestCasePass=Constants.TestCasePass+1;
}
else {
Constants.TestCaseFail=Constants.TestCaseFail+1;
}
System.out.println("TestCase Pass=="+Constants.TestCasePass+"TestCase Fail=="+Constants.TestCaseFail);
}
XSSFSheet TestSuiteSheet=ReportBook.getSheet("TestSuite");
for (int i=1;i<=TestSuiteSheet.getLastRowNum();i++){
String txt=Excel.GetString(TestSuiteSheet, i,1 );
if (txt.equals("PASS")){
Constants.TestSuitePass=Constants.TestSuitePass+1;
}
else {
Constants.TestSuiteFail=Constants.TestSuiteFail+1;
}
}
double TestStepRate=(double)Constants.TestStepPass/(Constants.TestStepPass+Constants.TestStepFail);
double TestCaseRate=(double)Constants.TestCasePass/(Constants.TestCasePass+Constants.TestCaseFail);
double TestSuiteRate=(double)Constants.TestSuitePass/(Constants.TestSuitePass+Constants.TestSuiteFail);
System.out.println("测试步骤通过率="+TestStepRate+"测试用例通过率=="+TestCaseRate+"测试套件通过率=="+TestSuiteRate);
XSSFSheet Test_Index_Sheet=ReportBook.getSheet("首页---数据统计");
//写入统计结果
if (Test_Index_Sheet!=null) {
XSSFRow Row=Test_Index_Sheet.getRow(2);
//数值型格式
XSSFCellStyle numStyle = ReportBook.createCellStyle();
numStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0"));
//双精度型格式
XSSFCellStyle doubleStyle = ReportBook.createCellStyle();
doubleStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));
//写入测试套件的结果
XSSFCell cell0=Row.createCell(0);
cell0.setCellValue(Constants.TestSuitePass);
cell0.setCellStyle(numStyle);
XSSFCell cell1=Row.createCell(1);
cell1.setCellValue(Constants.TestSuiteFail);
cell1.setCellStyle(numStyle);
//写入测试套件的通过率
XSSFCell cell2=Row.createCell(2);
cell2.setCellValue(TestSuiteRate);
cell2.setCellStyle(doubleStyle);
//写入测试用例的结果
XSSFCell cell3=Row.createCell(3);
cell3.setCellValue(Constants.TestCasePass);
cell3.setCellStyle(numStyle);
XSSFCell cell4=Row.createCell(4);
cell4.setCellValue(Constants.TestCaseFail);
cell4.setCellStyle(numStyle);
//写入测试用例的通过率
XSSFCell cell5=Row.createCell(5);
cell5.setCellValue(TestCaseRate);
cell5.setCellStyle(doubleStyle);
//写入测试步骤的结果
XSSFCell cell6=Row.createCell(6);
cell6.setCellValue(Constants.TestStepPass);
cell6.setCellStyle(numStyle);
XSSFCell cell7=Row.createCell(7);
cell7.setCellValue(Constants.TestStepFail);
cell7.setCellStyle(numStyle);
//写入测试步骤的通过率
XSSFCell cell8=Row.createCell(8);
cell8.setCellValue(TestStepRate);
cell8.setCellStyle(doubleStyle);
//将执行时间写入到对应单元格中
// Row.createCell(9).setCellValue(Constants.min);
//创建文件输出流
FileOutputStream fileOut = new FileOutputStream(TestReport);
// 写入文件输出流到Excel
ReportBook.write(fileOut);
//关闭文件流
fileOut.close();
//释放Excel
ReportFile.close();
}
else {
System.out.println("首页统计的Sheet 不存在");
}
}
上面那个模板因为有Excel 中的图表,不能删除,如果每次运行后怎么将历史数据清空呢,我们可以将清空数据放在脚本启动时也可以放在统计报告之前
清空操作逻辑是先将前一次的测试报告全数复制一份成以当前时间格式命名的文件
![](https://img.haomeiwen.com/i2286822/1c4205d8a3740cbf.png)
复制之后再清空除首页--统计报表之外的所有Sheet 报表
代码如下
//清除旧记录
public static void CleanData() throws IOException {
String TestReport=System.getProperty("user.dir")+"\\测试报告\\测试报告.xlsx";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
//生成时间戳
String dateString = formatter.format(new Date());
File SourceFile=new File(TestReport);
File Destination=new File(System.getProperty("user.dir")+"\\测试报告\\"+dateString+".xlsx");
FileUtils.copyFile(SourceFile, Destination);
FileInputStream ReportFile = new FileInputStream(TestReport);
//创建工作本
try {
XSSFWorkbook ReportBook = new XSSFWorkbook(ReportFile);
//移除Sheet
if (ReportBook.getSheet("TestStep")!=null) {
ReportBook.removeSheetAt(ReportBook.getSheetIndex("TestStep"));
}
if (ReportBook.getSheet("TestCase")!=null) {
ReportBook.removeSheetAt(ReportBook.getSheetIndex("TestCase"));
}
if (ReportBook.getSheet("TestSuite")!=null) {
ReportBook.removeSheetAt(ReportBook.getSheetIndex("TestSuite"));
}
//创建文件输出流
FileOutputStream fileOut = new FileOutputStream(TestReport);
// 写入文件输出流到Excel
ReportBook.write(fileOut);
//关闭文件流
fileOut.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//释放Excel
ReportFile.close();
}
网友评论