Skip to content

Commit

Permalink
Honour Custom Attributes in default reports
Browse files Browse the repository at this point in the history
Closes #2725
  • Loading branch information
krmahadevan committed May 15, 2022
1 parent 2be448a commit 3b664ec
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Current
7.6.0
Fixed: GITHUB-2741: Show fully qualified name of the test instead of just the function name for better readability of test output.(Krishnan Mahadevan)
Fixed: GITHUB-2725: Honour custom attribute values in TestNG default reports (Krishnan Mahadevan)
Fixed: GITHUB-2726: @AfterClass config method is executed for EACH @Test method when parallel == methods (Krishnan Mahadevan)
Fixed: GITHUB-2752: TestListener is being lost when implenting both IClassListener and ITestListener (Krishnan Mahadevan)
New: GITHUB-2724: DataProvider: possibility to unload dataprovider class, when done with it (Dzmitry Sankouski)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class XMLReporterConfig implements IReporterConfig {
public static final String TAG_ATTRIBUTES = "attributes";
public static final String TAG_ATTRIBUTE = "attribute";

public static final String TAG_CUSTOM_ATTRIBUTES = "custom-attributes";
public static final String TAG_CUSTOM_ATTRIBUTE = "custom-attribute";
public static final String TAG_CUSTOM_ATTRIBUTE_NAME = "name";
public static final String TAG_CUSTOM_ATTRIBUTE_VALUE = "value";

public static final String ATTR_URL = "url";
public static final String ATTR_NAME = "name";
public static final String ATTR_STATUS = "status";
Expand Down
20 changes: 20 additions & 0 deletions testng-core/src/main/java/org/testng/internal/BaseTestMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.testng.IClass;
import org.testng.IRetryAnalyzer;
import org.testng.ITestClass;
import org.testng.ITestNGMethod;
import org.testng.ITestObjectFactory;
import org.testng.ITestResult;
import org.testng.annotations.CustomAttribute;
import org.testng.annotations.ITestOrConfiguration;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
Expand Down Expand Up @@ -466,11 +468,29 @@ private String computeSignature() {
.append(", instance:")
.append(getInstance())
.append(instanceParameters())
.append(customAttributes())
.append("]");

return result.toString();
}

private String customAttributes() {
CustomAttribute[] attributes = getAttributes();
if (attributes == null || attributes.length == 0) {
return "";
}
return ", attributes: "
+ Arrays.stream(this.getAttributes())
.map(
attribute ->
"<name: "
+ attribute.name()
+ ", value:"
+ Arrays.toString(attribute.values())
+ ">")
.collect(Collectors.joining(", "));
}

public String getSimpleName() {
return m_method.getDeclaringClass().getSimpleName() + "." + m_method.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.CustomAttribute;
import org.testng.collections.Lists;
import org.testng.internal.Utils;
import org.testng.log4testng.Logger;
Expand Down Expand Up @@ -288,6 +289,22 @@ private void generateForResult(ITestResult ans) {
}
m_out.println("</tr>");
}
CustomAttribute[] attributes = ans.getMethod().getAttributes();
boolean hasCustomAttributes = attributes != null && attributes.length != 0;
if (hasCustomAttributes) {
tableStart("result", null);
m_out.print("<tr class=\"param\">");
for (int x = 1; x <= attributes.length; x++) {
m_out.print("<th>Attribute #" + x + "</th>");
}
m_out.println("</tr>");
m_out.print("<tr class=\"param stripe\">");
for (CustomAttribute p : attributes) {
String text = String.format("name=%s, value= %s", p.name(), Arrays.toString(p.values()));
m_out.println("<td>" + Utils.escapeHtml(text) + "</td>");
}
m_out.println("</tr>");
}
List<String> msgs = Reporter.getOutput(ans);
boolean hasReporterOutput = !msgs.isEmpty();
Throwable exception = ans.getThrowable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
Expand All @@ -19,6 +20,7 @@
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.CustomAttribute;
import org.testng.collections.Lists;
import org.testng.internal.Utils;
import org.testng.log4testng.Logger;
Expand Down Expand Up @@ -445,15 +447,12 @@ private void writeScenario(int scenarioIndex, String label, ITestResult result)

writer.print("<table class=\"result\">");

boolean hasRows = false;

// Write test parameters (if any)
Object[] parameters = result.getParameters();
boolean hasRows = dumpParametersInfo("Factory Parameter", result.getFactoryParameters());
int parameterCount = (parameters == null ? 0 : parameters.length);
hasRows = dumpParametersInfo("Factory Parameter", result.getFactoryParameters());
parameters = result.getParameters();
parameterCount = (parameters == null ? 0 : parameters.length);
hasRows = dumpParametersInfo("Parameter", result.getParameters());
dumpAttributesInfo("Attribute(s)", result.getMethod().getAttributes());

// Write reporter messages (if any)
List<String> reporterMessages = Reporter.getOutput(result);
Expand Down Expand Up @@ -529,6 +528,27 @@ private boolean dumpParametersInfo(String prefix, Object[] parameters) {
return true;
}

private void dumpAttributesInfo(String prefix, CustomAttribute[] attributes) {
int parameterCount = (attributes == null ? 0 : attributes.length);
if (parameterCount == 0) {
return;
}
writer.print("<tr class=\"param\">");
writer.print(String.format("<th colspan=3>%s</th>", prefix));
writer.print("</tr>");
writer.print("<tr class=\"param stripe\">");
writer.print("<th>#</th><th>Name</th><th>Value(s)</th>");
writer.print("</tr>");
int i = 1;
for (CustomAttribute attribute : attributes) {
writer.print("<tr>");
writer.print("<td>" + String.format("%02d.", (i++)) + "</td>");
writer.print("<td>" + attribute.name() + "</td>");
writer.print("<td>" + Utils.escapeHtml(Arrays.toString(attribute.values())) + "</td>");
writer.print("</tr>");
}
}

protected void writeReporterMessages(List<String> reporterMessages) {
writer.print("<div class=\"messages\">");
Iterator<String> iterator = reporterMessages.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
Expand All @@ -13,6 +14,7 @@
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestRunner;
import org.testng.annotations.CustomAttribute;
import org.testng.internal.Utils;
import org.testng.log4testng.Logger;

Expand Down Expand Up @@ -67,6 +69,7 @@ public static void generateTable(
.append("</b></td></tr>\n")
.append("<tr>")
.append("<td><b>Test method</b></td>\n")
.append("<td><b>Attribute(s)</b></td>\n")
.append("<td width=\"30%\"><b>Exception</b></td>\n")
.append("<td width=\"10%\"><b>Time (seconds)</b></td>\n")
.append("<td><b>Instance</b></td>\n")
Expand Down Expand Up @@ -129,7 +132,7 @@ public static void generateTable(
//
{
List<String> output = Reporter.getOutput(tr);
if (null != output && output.size() > 0) {
if (!output.isEmpty()) {
pw.append("<br/>");
// Method name
String divId = "Output-" + tr.hashCode();
Expand All @@ -156,6 +159,35 @@ public static void generateTable(

pw.append("</td>\n");

// Custom attributes
CustomAttribute[] attributes = tr.getMethod().getAttributes();
if (attributes != null && attributes.length > 0) {
pw.append("<td>");
String divId = "attributes-" + tr.hashCode();
pw.append("\n<a href=\"#")
.append(divId)
.append("\"")
.append(" onClick='toggleBox(\"")
.append(divId)
.append("\", this, \"Show attributes\", \"Hide attributes\");'>")
.append("Show attributes</a>\n")
.append("\n<a href=\"#")
.append(divId)
.append("\"></a>");
pw.append("<div class='log' id=\"").append(divId).append("\">\n");
Arrays.stream(attributes)
.map(
attribute ->
"name: " + attribute.name() + ", value:" + Arrays.toString(attribute.values()))
.forEach(
line -> {
pw.append(Utils.escapeHtml(line));
pw.append("<br>");
});
pw.append("</div>");
pw.append("</td>");
}

// Exception
tw = tr.getThrowable();
String stackTrace;
Expand Down
16 changes: 16 additions & 0 deletions testng-core/src/main/java/org/testng/reporters/TextReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.testng.internal.Utils.isStringNotBlank;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
Expand All @@ -11,6 +12,7 @@
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.CustomAttribute;
import org.testng.collections.Lists;
import org.testng.internal.Utils;

Expand Down Expand Up @@ -52,6 +54,7 @@ private void logResults(ITestContext context) {
"FAILED CONFIGURATION",
Utils.detailedMethodName(tr.getMethod()),
tr.getMethod().getDescription(),
tr.getMethod().getAttributes(),
stackTrace,
tr.getParameters(),
tr.getMethod().getParameterTypes());
Expand All @@ -63,6 +66,7 @@ private void logResults(ITestContext context) {
"SKIPPED CONFIGURATION",
Utils.detailedMethodName(tr.getMethod()),
tr.getMethod().getDescription(),
tr.getMethod().getAttributes(),
null,
tr.getParameters(),
tr.getMethod().getParameterTypes());
Expand Down Expand Up @@ -131,6 +135,7 @@ private void logResult(String status, ITestResult tr, String stackTrace) {
status,
tr.getMethod().getQualifiedName(),
tr.getMethod().getDescription(),
tr.getMethod().getAttributes(),
stackTrace,
tr.getParameters(),
tr.getMethod().getParameterTypes());
Expand Down Expand Up @@ -158,6 +163,7 @@ private void logResult(
String status,
String name,
String description,
CustomAttribute[] attributes,
String stackTrace,
Object[] params,
Class<?>[] paramTypes) {
Expand Down Expand Up @@ -195,6 +201,16 @@ private void logResult(
}
msg.append(description);
}
if (attributes != null && attributes.length != 0) {
msg.append("\nTest Attributes: ");
String testAttributes =
Arrays.stream(attributes)
.map(
attribute ->
"<" + attribute.name() + ", " + Arrays.toString(attribute.values()) + ">")
.collect(Collectors.joining(", "));
msg.append(testAttributes).append("\n");
}
if (!Utils.isStringEmpty(stackTrace)) {
msg.append("\n").append(stackTrace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import org.testng.IConfigurationListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.annotations.CustomAttribute;
import org.testng.internal.Utils;

/**
Expand Down Expand Up @@ -232,6 +234,17 @@ private void logTestResult(Status st, ITestResult itr, boolean isConfMethod) {
sb.append("%");
}
}
CustomAttribute[] attributes = tm.getAttributes();
if ((st != Status.STARTED) && (attributes != null && attributes.length != 0)) {
String text =
"Test Attributes: "
+ Arrays.stream(attributes)
.map(
attribute ->
"<" + attribute.name() + ", " + Arrays.toString(attribute.values()) + ">")
.collect(Collectors.joining(","));
sb.append("\n").append(text);
}
log(sb.toString());
}

Expand Down
Loading

0 comments on commit 3b664ec

Please sign in to comment.