java测试报告html_testng生成自定义html报告

1 package com.reporter.main;

2

3 import java.io.BufferedWriter;

4 import java.io.FileWriter;

5 import java.io.Writer;

6 import java.util.List;

7 import java.util.Map;

8 import java.util.Properties;

9 import org.apache.velocity.Template;

10 import org.apache.velocity.VelocityContext;

11 import org.apache.velocity.app.VelocityEngine;

12 import org.testng.IReporter;

13 import org.testng.IResultMap;

14 import org.testng.ISuite;

15 import org.testng.ISuiteResult;

16 import org.testng.ITestContext;

17 import org.testng.ITestResult;

18 import org.testng.xml.XmlSuite;

19

20 public class GenerateReporter implements IReporter {

21     @Override

22     public void generateReport(List xmlSuites, List suites,

23             String outputDirectory) {

24         // TODO Auto-generated method stub

25         try {

26             // 初始化并取得Velocity引擎

27             VelocityEngine ve = new VelocityEngine();

28             Properties p = new Properties();

29             //虽然不懂为什么这样设置,但结果是好的.可以用了

30             p.setProperty("resource.loader", "class");

31             p.setProperty("class.resource.loader.class","org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

32             ve.init(p);

33             Template t = ve.getTemplate("com/reporter/VMmodel/overview.vm");

34             VelocityContext context = new VelocityContext();

35

36             for (ISuite suite : suites) {

37                 Map suiteResults = suite.getResults();

38                 for (ISuiteResult suiteResult : suiteResults.values()) {

39                     ReporterData data = new ReporterData();

40                     ITestContext testContext = suiteResult.getTestContext();

41                     // 把数据填入上下文

42                     context.put("overView", data.testContext(testContext));//测试结果汇总信息

43                     //ITestNGMethod[] allTests = testContext.getAllTestMethods();//所有的测试方法

44                     //Collection excludeTests = testContext.getExcludedMethods();//未执行的测试方法

45                     IResultMap passedTests = testContext.getPassedTests();//测试通过的测试方法

46                     IResultMap failedTests = testContext.getFailedTests();//测试失败的测试方法

47                     IResultMap skippedTests = testContext.getSkippedTests();//测试跳过的测试方法

48

49                     context.put("pass", data.testResults(passedTests, ITestResult.SUCCESS));

50                     context.put("fail", data.testResults(failedTests, ITestResult.FAILURE));

51                     context.put("skip", data.testResults(skippedTests, ITestResult.FAILURE));

52

53

54

55                 }

56             }

57             // 输出流

58             //Writer writer = new BufferedWriter(new FileWriter("report.html"));

59             OutputStream out=new FileOutputStream("report.html");

60             Writer writer = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));//解决乱码问题

61             // 转换输出

62             t.merge(context, writer);

63             //System.out.println(writer.toString());

64             writer.flush();

65         } catch (Exception e) {

66             // TODO Auto-generated catch block

67             e.printStackTrace();

68         }

69     }

70

71

72 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TestNG默认的测试报告是以XML格式输出的,如果想要生成HTML格式的测试报告,可以使用TestNG自带的`Reporter`类或第三方工具,如ExtentReports、Allure等。以下是使用Reporter类生成HTML格式测试报告的步骤: 1. 在TestNG测试类中添加`@Listeners(TestReportListener.class)`注解,引入自定义的监听器类。 2. 创建一个实现`ITestListener`接口的监听器类,重写`onTestSuccess`、`onTestFailure`、`onTestSkipped`等方法,在方法中使用`Reporter.log`方法输出测试结果。 3. 运行测试后,会在项目目录下生成一个`test-output`文件夹,其中包含了测试结果的XML文件和截图(如果有的话)。 4. 使用TestNG自带的`Reporter`类将XML文件转换为HTML格式的测试报告,代码如下: ```java import org.testng.Reporter; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; public class TestReportListener implements ITestListener { @BeforeSuite public void beforeSuite() { Reporter.log("<html><head><title>Test Report</title></head><body>"); } @AfterSuite public void afterSuite() { Reporter.log("</body></html>"); Reporter.flush(); } @Override public void onTestSuccess(ITestResult result) { Reporter.log("<p style=\"color:green;\">Test Passed: " + result.getName() + "</p>"); } @Override public void onTestFailure(ITestResult result) { Reporter.log("<p style=\"color:red;\">Test Failed: " + result.getName() + "</p>"); } @Override public void onTestSkipped(ITestResult result) { Reporter.log("<p style=\"color:orange;\">Test Skipped: " + result.getName() + "</p>"); } // 将其他方法省略... } ``` 在`beforeSuite`方法中输出HTML文件的头部,`afterSuite`方法中输出HTML文件的尾部,并调用`Reporter.flush()`方法将结果写入HTML文件。在各个测试结果的处理方法中,使用`Reporter.log`方法输出HTML格式的测试结果。 5. 运行测试后,在`test-output`文件夹中会生成一个`emailable-report.html`文件,即为HTML格式的测试报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值