Пользовательский репортер TestNG - добавление трассировки стека в отчет - PullRequest
1 голос
/ 04 июля 2019

Я использую собственный репортер для моего комплекта 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>

1 Ответ

0 голосов
/ 05 июля 2019
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];
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...