【问题标题】:TestNG custom reporter - adding stacktrace to reportTestNG 自定义报告器 - 将堆栈跟踪添加到报告
【发布时间】:2019-07-04 09:24:24
【问题描述】:

我正在为我的 TestNG 套件使用自定义报告器。我借用了网上教程的基本代码,做了一些调整。但是,我想为任何失败的测试包含一个堆栈跟踪,就像内置报告一样 (emailable-report.html)。只需将本报告中看到的内容附加到我的底部就可以了。

谁能给我任何关于如何实现这一点的指示?我不知道如何访问堆栈跟踪。

这是 generateReport 方法(我可以展示更多内容,但可能不相关):

@Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {

        try {
            // Get content data in TestNG report template file.
            String customReportTemplateStr = this.readEmailableReportTemplate();

            // Create custom report title.
            String customReportTitle = this.getCustomReportTitle(Base.Client + " Regression Suite Report");

            // Create test suite summary data.
            String customSuiteSummary = this.getTestSuiteSummary(suites);

            // Create test methods summary data.
            String customTestMethodSummary = this.getTestMethodSummary(suites);

            // Replace report title place holder with custom title.
            customReportTemplateStr = customReportTemplateStr.replaceAll("\\$TestNG_Custom_Report_Title\\$",
                    customReportTitle);

            // Replace test suite place holder with custom test suite summary.
            customReportTemplateStr = customReportTemplateStr.replaceAll("\\$Test_Case_Summary\\$", customSuiteSummary);

            // Replace test methods place holder with custom test method summary.
            customReportTemplateStr = customReportTemplateStr.replaceAll("\\$Test_Case_Detail\\$",
                    customTestMethodSummary);

            // Write replaced test report content to custom-emailable-report.html.
            File targetFile = new File(outputDirectory + "/custom-emailable-report.html");
            FileWriter fw = new FileWriter(targetFile);
            fw.write(customReportTemplateStr);
            fw.flush();
            fw.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

...这是 html 模板:

<body>
    <table>
      <tr><center><font size="5" face="verdana">
        <b>$TestNG_Custom_Report_Title$</b>
      </font></center></tr>
      <p></p>
      <thead>
        <tr>
          <th># Total Method</th>
          <th># Passed</th>
          <th># Skipped</th>
          <th># Failed</th>
          <th>Start Time</th>
          <th>End Time</th>
          <th>Execute Time (hh:mm:ss)</th>
        </tr>
       </thead> 
       $Test_Case_Summary$
    </table>
    <table id="summary">
      <thead>
        <tr>
          <th>Class</th>
          <th>Method</th>
          <th>Start Time</th>
          <th>Execution Time (hh:mm:ss)</th>
          <th>Browser</th>
          <th>Screenshot</th>
        </tr>
      </thead> 
      $Test_Case_Detail$
    </table>
  </body>

【问题讨论】:

    标签: java selenium testng reporting


    【解决方案1】:
    import org.testng.internal.Utils;
    
    private void generateResult(ITestResult ans){
        ITestResult ans;
        List<String> msgs = Reporter.getOutput(ans);
        Throwable exception = ans.getThrowable();
    
        boolean hasThrowable = exception != null;
        if (hasThrowable) {
           String stackTraceMsg = Utils.stackTrace(exception, true)[0];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-16
      • 2011-07-27
      • 2011-03-03
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多