All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testng.reporters.TestHTMLReporter Maven / Gradle / Ivy

There is a newer version: 7.10.1
Show newest version
package org.testng.reporters;

import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import org.testng.TestRunner;
import org.testng.internal.Utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * This class implements an HTML reporter for individual tests.
 */
public class TestHTMLReporter extends TestListenerAdapter {
  private static final Comparator NAME_COMPARATOR= new NameComparator();
  private static final Comparator CONFIGURATION_COMPARATOR= new ConfigurationComparator();

  private ITestContext m_testContext = null;

  /////
  // implements ITestListener
  //
  @Override
  public void onStart(ITestContext context) {
    m_testContext = context;
  }

  @Override
  public void onFinish(ITestContext context) {
    generateLog(m_testContext,
                null /* host */,
                m_testContext.getOutputDirectory(),
                getConfigurationFailures(),
                getConfigurationSkips(),
                getPassedTests(),
                getFailedTests(),
                getSkippedTests(),
                getFailedButWithinSuccessPercentageTests());
  }
  //
  // implements ITestListener
  /////

  private static String getOutputFile(ITestContext context) {
    return context.getName() + ".html";
  }

  public static void generateTable(PrintWriter pw, String title,
      Collection tests, String cssClass, Comparator comparator)
  {
    pw.append("\n")
      .append("\n")
      .append("")
      .append("\n")
      .append("\n")
      .append("\n")
      .append("\n")
      .append("\n");

    if (tests instanceof List) {
      Collections.sort((List) tests, comparator);
    }

    // User output?
    String id;
    Throwable tw;

    for (ITestResult tr : tests) {
      pw.append("\n");

      // Test method
      ITestNGMethod method = tr.getMethod();

      String name = method.getMethodName();
      pw.append("\n");


      // Exception
      tw = tr.getThrowable();
      String stackTrace;
      String fullStackTrace;

      id = "stack-trace" + tr.hashCode();
      pw.append("\n");

      // Time
      long time = (tr.getEndMillis() - tr.getStartMillis()) / 1000;
      String strTime = Long.toString(time);
      pw.append("\n");

      // Instance
      Object instance = tr.getInstance();
      pw.append("");

      pw.append("\n");
    }

    pw.append("
").append(title).append("
Test methodExceptionTime (seconds)Instance
") .append("").append(name).append(""); // Test class String testClass = tr.getTestClass().getName(); if (testClass != null) { pw.append("
").append("Test class: ").append(testClass); // Test name String testName = tr.getTestName(); if (testName != null) { pw.append(" (").append(testName).append(")"); } } // Method description if (! Utils.isStringEmpty(method.getDescription())) { pw.append("
").append("Test method: ").append(method.getDescription()); } Object[] parameters = tr.getParameters(); if (parameters != null && parameters.length > 0) { pw.append("
Parameters: "); for (int j = 0; j < parameters.length; j++) { if (j > 0) { pw.append(", "); } pw.append(parameters[j] == null ? "null" : parameters[j].toString()); } } // // Output from the method, created by the user calling Reporter.log() // { List output = Reporter.getOutput(tr); if (null != output && output.size() > 0) { pw.append("
"); // Method name String divId = "Output-" + tr.hashCode(); pw.append("\n") .append("Show output\n") .append("\nShow all outputs\n") ; // Method output pw.append("
\n"); for (String s : output) { pw.append(s).append("
\n"); } pw.append("
\n"); } } pw.append("
"); if (null != tw) { fullStackTrace = Utils.longStackTrace(tw, true); stackTrace = "
" + Utils.shortStackTrace(tw, true)  + "
"; pw.append(stackTrace); // JavaScript link pw.append("") .append("Click to show all stack frames").append("\n") .append("
") .append("
").append(fullStackTrace).append("
") .append("
") ; } pw.append("
").append(strTime).append("").append(Objects.toString(instance)).append("

\n"); } private static String arrayToString(String[] array) { StringBuilder result = new StringBuilder(); for (String element : array) { result.append(element).append(" "); } return result.toString(); } private static final String HEAD = "\n\n" + "\n" + "\n"; public static void generateLog(ITestContext testContext, String host, String outputDirectory, Collection failedConfs, Collection skippedConfs, Collection passedTests, Collection failedTests, Collection skippedTests, Collection percentageTests) { try (PrintWriter writer = new PrintWriter(Utils.openWriter(outputDirectory, getOutputFile(testContext)))) { writer.append("\n\n") .append("TestNG: ").append(testContext.getName()).append("\n") .append(HtmlHelper.getCssString()) .append(HEAD) .append("\n") .append("\n"); Date startDate = testContext.getStartDate(); Date endDate = testContext.getEndDate(); long duration = (endDate.getTime() - startDate.getTime()) / 1000; int passed = testContext.getPassedTests().size() + testContext.getFailedButWithinSuccessPercentageTests().size(); int failed = testContext.getFailedTests().size(); int skipped = testContext.getSkippedTests().size(); String hostLine = Utils.isStringEmpty(host) ? "" : "Remote host:" + host + "\n"; writer .append("

").append(testContext.getName()).append("

") .append("\n") .append("\n") // .append("\n") // .append("\n") .append("\n") .append("\n") .append("\n") .append("\n") .append(hostLine) .append("\n") .append("\n") .append("\n") .append("\n") .append("\n") .append("\n") .append("
Property file:").append(m_testRunner.getPropertyFileName()).append("
Tests passed/Failed/Skipped:").append(Integer.toString(passed)).append("/").append(Integer.toString(failed)).append("/").append(Integer.toString(skipped)).append("
Started on:").append(testContext.getStartDate().toString()).append("
Total time:").append(Long.toString(duration)).append(" seconds (").append(Long.toString(endDate.getTime() - startDate.getTime())) .append(" ms)
Included groups:").append(arrayToString(testContext.getIncludedGroups())).append("
Excluded groups:").append(arrayToString(testContext.getExcludedGroups())).append("

\n"); writer.append("(Hover the method name to see the test class name)

\n"); if (!failedConfs.isEmpty()) { generateTable(writer, "FAILED CONFIGURATIONS", failedConfs, "failed", CONFIGURATION_COMPARATOR); } if (!skippedConfs.isEmpty()) { generateTable(writer, "SKIPPED CONFIGURATIONS", skippedConfs, "skipped", CONFIGURATION_COMPARATOR); } if (!failedTests.isEmpty()) { generateTable(writer, "FAILED TESTS", failedTests, "failed", NAME_COMPARATOR); } if (!percentageTests.isEmpty()) { generateTable(writer, "FAILED TESTS BUT WITHIN SUCCESS PERCENTAGE", percentageTests, "percent", NAME_COMPARATOR); } if (!passedTests.isEmpty()) { generateTable(writer, "PASSED TESTS", passedTests, "passed", NAME_COMPARATOR); } if (!skippedTests.isEmpty()) { generateTable(writer, "SKIPPED TESTS", skippedTests, "skipped", NAME_COMPARATOR); } writer.append("\n"); } catch (IOException e) { if (TestRunner.getVerbose() > 1) { e.printStackTrace(); } else { ppp(e.getMessage()); } } } private static void ppp(String s) { System.out.println("[TestHTMLReporter] " + s); } private static class NameComparator implements Comparator { @Override public int compare(ITestResult o1, ITestResult o2) { String c1 = o1.getMethod().getMethodName(); String c2 = o2.getMethod().getMethodName(); return c1.compareTo(c2); } } private static class ConfigurationComparator implements Comparator { @Override public int compare(ITestResult o1, ITestResult o2) { ITestNGMethod tm1= o1.getMethod(); ITestNGMethod tm2= o2.getMethod(); return annotationValue(tm2) - annotationValue(tm1); } private static int annotationValue(ITestNGMethod method) { if(method.isBeforeSuiteConfiguration()) { return 10; } if(method.isBeforeTestConfiguration()) { return 9; } if(method.isBeforeClassConfiguration()) { return 8; } if(method.isBeforeGroupsConfiguration()) { return 7; } if(method.isBeforeMethodConfiguration()) { return 6; } if(method.isAfterMethodConfiguration()) { return 5; } if(method.isAfterGroupsConfiguration()) { return 4; } if(method.isAfterClassConfiguration()) { return 3; } if(method.isAfterTestConfiguration()) { return 2; } if(method.isAfterSuiteConfiguration()) { return 1; } return 0; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy