org.testng.reporters.TestHTMLReporter Maven / Gradle / Ivy
Show all versions of testng Show documentation
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.internal.Utils;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
* This class implements an HTML reporter for individual tests.
*
* @author Cedric Beust, May 2, 2004
* @author Alexandru Popescu
*/
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(StringBuffer sb, String title,
Collection tests, String cssClass, Comparator comparator)
{
sb.append("\n")
.append("").append(title).append(" \n")
.append("")
.append("Test method \n")
.append("Exception \n")
.append("Time (seconds) \n")
.append("Instance \n")
.append(" \n");
if (tests instanceof List) {
Collections.sort((List) tests, comparator);
}
// User output?
String id = "";
Throwable tw = null;
for (ITestResult tr : tests) {
sb.append("\n");
// Test method
ITestNGMethod method = tr.getMethod();
String name = method.getMethodName();
sb.append("")
.append("").append(name).append("");
// Test class
String testClass = tr.getTestClass().getName();
if (testClass != null) {
sb.append("
").append("Test class: " + testClass);
// Test name
String testName = tr.getTestName();
if (testName != null) {
sb.append(" (").append(testName).append(")");
}
}
// Method description
if (! Utils.isStringEmpty(method.getDescription())) {
sb.append("
").append("Test method: ").append(method.getDescription());
}
Object[] parameters = tr.getParameters();
if (parameters != null && parameters.length > 0) {
sb.append("
Parameters: ");
for (int j = 0; j < parameters.length; j++) {
if (j > 0) {
sb.append(", ");
}
sb.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) {
sb.append("
");
// Method name
String divId = "Output-" + tr.hashCode();
sb.append("\n")
.append("Show output\n")
.append("\nShow all outputs\n")
;
// Method output
sb.append("\n");
for (String s : output) {
sb.append(s).append("
\n");
}
sb.append("\n");
}
}
sb.append(" \n");
// Exception
tw = tr.getThrowable();
String stackTrace = "";
String fullStackTrace = "";
id = "stack-trace" + tr.hashCode();
sb.append("");
if (null != tw) {
String[] stackTraces = Utils.stackTrace(tw, true);
fullStackTrace = stackTraces[1];
stackTrace = "" + stackTraces[0] + "
";
sb.append(stackTrace);
// JavaScript link
sb.append("")
.append("Click to show all stack frames").append("\n")
.append("")
.append("" + fullStackTrace + "
")
.append("")
;
}
sb.append(" \n");
// Time
long time = (tr.getEndMillis() - tr.getStartMillis()) / 1000;
String strTime = Long.toString(time);
sb.append("").append(strTime).append(" \n");
// Instance
Object instance = tr.getInstance();
sb.append("").append(instance).append(" ");
sb.append(" \n");
}
sb.append("
\n");
}
private static String arrayToString(String[] array) {
StringBuffer result = new StringBuffer("");
for (String element : array) {
result.append(element).append(" ");
}
return result.toString();
}
private static 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)
{
StringBuffer sb = new StringBuffer();
sb.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 ";
sb
.append("").append(testContext.getName()).append("
")
.append("\n")
.append("\n")
// .append("Property file: ").append(m_testRunner.getPropertyFileName()).append(" \n")
// .append(" \n")
.append("Tests passed/Failed/Skipped: ").append(passed).append("/").append(failed).append("/").append(skipped).append(" \n")
.append(" \n")
.append("Started on: ").append(testContext.getStartDate().toString()).append(" \n")
.append(" \n")
.append(hostLine)
.append("Total time: ").append(duration).append(" seconds (").append(endDate.getTime() - startDate.getTime())
.append(" ms) \n")
.append("\n")
.append("Included groups: ").append(arrayToString(testContext.getIncludedGroups())).append(" \n")
.append(" \n")
.append("Excluded groups: ").append(arrayToString(testContext.getExcludedGroups())).append(" \n")
.append(" \n")
.append("
\n")
;
sb.append("(Hover the method name to see the test class name)\n");
if (failedConfs.size() > 0) {
generateTable(sb, "FAILED CONFIGURATIONS", failedConfs, "failed", CONFIGURATION_COMPARATOR);
}
if (skippedConfs.size() > 0) {
generateTable(sb, "SKIPPED CONFIGURATIONS", skippedConfs, "skipped", CONFIGURATION_COMPARATOR);
}
if (failedTests.size() > 0) {
generateTable(sb, "FAILED TESTS", failedTests, "failed", NAME_COMPARATOR);
}
if (percentageTests.size() > 0) {
generateTable(sb, "FAILED TESTS BUT WITHIN SUCCESS PERCENTAGE",
percentageTests, "percent", NAME_COMPARATOR);
}
if (passedTests.size() > 0) {
generateTable(sb, "PASSED TESTS", passedTests, "passed", NAME_COMPARATOR);
}
if (skippedTests.size() > 0) {
generateTable(sb, "SKIPPED TESTS", skippedTests, "skipped", NAME_COMPARATOR);
}
sb.append("\n");
Utils.writeFile(outputDirectory, getOutputFile(testContext), sb.toString());
}
private static void ppp(String s) {
System.out.println("[TestHTMLReporter] " + s);
}
private static class NameComparator implements Comparator, Serializable {
private static final long serialVersionUID = 381775815838366907L;
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, Serializable {
private static final long serialVersionUID = 5558550850685483455L;
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;
}
}
}