java编写测试脚本语言_使用 Java 语言编写测试脚本测试App

package com01; //修改为正确的包名

importjava.io.File;importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;importjava.nio.file.StandardOpenOption;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importorg.testng.ITestContext;importorg.testng.ITestResult;importorg.testng.TestListenerAdapter;public class TestReport extendsTestListenerAdapter{privateString reportPath;

@Overridepublic voidonStart(ITestContext context) {

File htmlReportDir= new File("test-output");if (!htmlReportDir.exists()) {

htmlReportDir.mkdirs();

}

String reportName= formateDate()+"_result.html";

reportPath= htmlReportDir+"/"+reportName;

File report= newFile(htmlReportDir,reportName);if(report.exists()){try{

report.createNewFile();

}catch(IOException e) {

e.printStackTrace();

}

}

StringBuilder sb= new StringBuilder("

"

+ "

自动化测试报告"

+ "

测试用例运行结果列表

"

+ "

+ ""

+ "

"

+ "

测试用例名"

+ "

测试用例结果"

+ "

"

+ "

"

+ "

");

String res=sb.toString();try{

Files.write((Paths.get(reportPath)),res.getBytes("utf-8"));

}catch(IOException e) {

e.printStackTrace();

}

}

@Overridepublic voidonTestSuccess(ITestResult result) {

StringBuilder sb= new StringBuilder("

");

sb.append(result.getMethod().getRealClass()+"."+result.getMethod().getMethodName());

sb.append("

Passed");

String res=sb.toString();try{

Files.write((Paths.get(reportPath)),res.getBytes("utf-8"),StandardOpenOption.APPEND);

}catch(IOException e) {

e.printStackTrace();

}

}

@Overridepublic voidonTestSkipped(ITestResult result) {

StringBuilder sb= new StringBuilder("

");

sb.append(result.getMethod().getRealClass()+"."+result.getMethod().getMethodName());

sb.append("

Skipped");

sb.append("

测试用例跳过,原因:
");

sb.append("");

Throwable throwable=result.getThrowable();

sb.append(throwable.getMessage());

sb.append("

");

String res=sb.toString();try{

Files.write((Paths.get(reportPath)),res.getBytes("utf-8"),StandardOpenOption.APPEND);

}catch(IOException e) {

e.printStackTrace();

}

}

@Overridepublic voidonTestFailure(ITestResult result) {

StringBuilder sb= new StringBuilder("

");

sb.append(result.getMethod().getRealClass()+"."+result.getMethod().getMethodName());

sb.append("

Failed
");

sb.append("

测试用例执行失败,原因:
");

sb.append("");

Throwable throwable=result.getThrowable();

sb.append(throwable.getMessage());

sb.append("

");

String res=sb.toString();try{

Files.write((Paths.get(reportPath)),res.getBytes("utf-8"),StandardOpenOption.APPEND);

}catch(IOException e) {

e.printStackTrace();

}

}

@Overridepublic voidonFinish(ITestContext testContext) {

StringBuilder sb= new StringBuilder("

返回顶部
");

sb.append("");

String msg=sb.toString();try{

Files.write((Paths.get(reportPath)),msg.getBytes("utf-8"),StandardOpenOption.APPEND);

}catch(IOException e) {

e.printStackTrace();

}

}public staticString formateDate(){

SimpleDateFormat sf= new SimpleDateFormat("yyyyMMdd HHmmss");

Calendar cal=Calendar.getInstance();

Date date=cal.getTime();returnsf.format(date);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值