Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1673 enable generateTestResultAttributes using Maven #1681

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/org/testng/CommandLineArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class CommandLineArgs {
+ " exclude")
public String excludedGroups;

public static final String GENERATE_SUITE_ATTRIBUTES = "-generateSuiteAttributes";
@Parameter(names = GENERATE_SUITE_ATTRIBUTES, description ="Generate attributes of suite in xml report")
public Boolean generateSuiteAttributes = Boolean.FALSE;

public static final String GENERATE_TESTRESULT_ATTRIBUTES = "-generateTestResultAttributes";
@Parameter(names = GENERATE_TESTRESULT_ATTRIBUTES, description ="Generate attributes of test-method in xml report")
public Boolean generateTestResultAttributes = Boolean.FALSE;

public static final String OUTPUT_DIRECTORY = "-d";
@Parameter(names = OUTPUT_DIRECTORY, description ="Output directory")
public String outputDirectory;
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/testng/IReporter2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.testng;

import org.testng.xml.XmlSuite;

import java.util.List;

/**
* This interface can be implemented by clients to generate a report. Its method
* generateReport() will be invoked after all the suite have run and the parameters
* give all the test results that happened during that run.
*
* @author kanaduchi
* Feb 02, 2018
*/
public interface IReporter2 extends ITestNGListener {
/**
* Generate a report for the given suites with specified attributes
*/
void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, IAttributes attributes);
}
54 changes: 49 additions & 5 deletions src/main/java/org/testng/TestNG.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.testng.internal.SuiteRunnerMap;
import org.testng.internal.Systematiser;
import org.testng.internal.Utils;
import org.testng.internal.Attributes;
import org.testng.internal.Version;
import org.testng.internal.annotations.DefaultAnnotationTransformer;
import org.testng.internal.annotations.IAnnotationFinder;
Expand Down Expand Up @@ -136,6 +137,7 @@ public class TestNG {
private final Map<Class<? extends ITestListener>, ITestListener> m_testListeners = Maps.newHashMap();
private final Map<Class<? extends ISuiteListener>, ISuiteListener> m_suiteListeners = Maps.newHashMap();
private final Map<Class<? extends IReporter>, IReporter> m_reporters = Maps.newHashMap();
private final Map<Class<? extends IReporter2>, IReporter2> m_reporters2 = Maps.newHashMap();
private final Map<Class<? extends IDataProviderListener>, IDataProviderListener> m_dataProviderListeners = Maps.newHashMap();


Expand All @@ -149,6 +151,8 @@ public class TestNG {

private String m_defaultSuiteName=DEFAULT_COMMAND_LINE_SUITE_NAME;
private String m_defaultTestName=DEFAULT_COMMAND_LINE_TEST_NAME;
private Boolean m_generateSuiteAttributes = false;
private Boolean m_generateTestResultAttributes = false;

private Map<String, Integer> m_methodDescriptors = Maps.newHashMap();

Expand Down Expand Up @@ -219,6 +223,22 @@ public void setOutputDirectory(final String outputdir) {
}
}

/**
* Sets the possibility to generate TestResult attributes
* @param generateTestResultAttributes true / false.
*/
public void setGenerateTestResultAttributes(final Boolean generateTestResultAttributes) {
m_generateTestResultAttributes = generateTestResultAttributes;
}

/**
* Sets the possibility to generate Suite attributes
* @param generateSuiteAttributes true / false.
*/
public void setGenerateSuiteAttributes(final Boolean generateSuiteAttributes) {
m_generateSuiteAttributes = generateSuiteAttributes;
}

/**
* If this method is passed true before run(), the default listeners
* will not be used.
Expand Down Expand Up @@ -651,6 +671,10 @@ public void addListener(ITestNGListener listener) {
IReporter reporter = (IReporter) listener;
maybeAddListener(m_reporters, reporter);
}
if (listener instanceof IReporter2) {
IReporter2 reporter = (IReporter2) listener;
maybeAddListener(m_reporters2, reporter);
}
if (listener instanceof IAnnotationTransformer) {
setAnnotationTransformer((IAnnotationTransformer) listener);
}
Expand Down Expand Up @@ -851,12 +875,12 @@ private static void initializeCommandLineSuitesGroups(XmlSuite s,
initializeCommandLineSuitesGroups(child, hasIncludedGroups, m_includedGroups, hasExcludedGroups, m_excludedGroups);
}
}
private void addReporter(Class<? extends IReporter> r) {
if (!m_reporters.containsKey(r)) {
m_reporters.put(r, ClassHelper.newInstance(r));

private void addReporter(Class<? extends IReporter2> r) {
if (!m_reporters2.containsKey(r)) {
m_reporters2.put(r, ClassHelper.newInstance(r));
}
}

private void initializeDefaultListeners() {
this.exitCodeListener = new org.testng.internal.ExitCodeListener();
addListener((ITestNGListener) this.exitCodeListener);
Expand Down Expand Up @@ -1070,12 +1094,30 @@ private static void usage() {
}

private void generateReports(List<ISuite> suiteRunners) {

for (IReporter reporter : m_reporters.values()) {
try {
long start = System.currentTimeMillis();
reporter.generateReport(m_suites, suiteRunners, m_outputDir);
Utils.log("TestNG", 2, "Time taken by " + reporter + ": "
+ (System.currentTimeMillis() - start) + " ms");
+ (System.currentTimeMillis() - start) + " ms");
}
catch(Exception ex) {
System.err.println("[TestNG] Reporter " + reporter + " failed");
ex.printStackTrace(System.err);
}
}

for (IReporter2 reporter : m_reporters2.values()) {
try {
long start = System.currentTimeMillis();
IAttributes attributes = new Attributes();
attributes.setAttribute("defaultOutputDirectory", m_outputDir);
attributes.setAttribute("generateSuiteAttributes", m_generateSuiteAttributes);
attributes.setAttribute("generateTestResultAttributes", m_generateTestResultAttributes);
reporter.generateReport(m_suites, suiteRunners, attributes);
Utils.log("TestNG", 2, "Time taken by " + reporter + ": "
+ (System.currentTimeMillis() - start) + " ms");
}
catch(Exception ex) {
System.err.println("[TestNG] Reporter " + reporter + " failed");
Expand Down Expand Up @@ -1363,6 +1405,8 @@ protected void configure(CommandLineArgs cla) {
}

setOutputDirectory(cla.outputDirectory);
setGenerateSuiteAttributes(cla.generateSuiteAttributes);
setGenerateTestResultAttributes(cla.generateTestResultAttributes);

if (cla.testNames != null) {
setTestNames(Arrays.asList(cla.testNames.split(",")));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/testng/TestNGAntTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class TestNGAntTask extends Task {
private String m_objectFactory;
protected String m_testRunnerFactory;
private boolean m_delegateCommandSystemProperties = false;
private boolean m_generateSuiteAttributes = false;
private boolean m_generateTestResultAttributes = false;

protected Environment m_environment= new Environment();

Expand Down Expand Up @@ -563,6 +565,8 @@ protected List<String> createArguments() {
addStringIfNotBlank(argv, CommandLineArgs.TEST_NAME, m_testName);
addStringIfNotBlank(argv, CommandLineArgs.TEST_NAMES, m_testNames);
addStringIfNotBlank(argv, CommandLineArgs.METHODS, m_methods);
addBooleanIfTrue(argv, CommandLineArgs.GENERATE_SUITE_ATTRIBUTES, m_generateSuiteAttributes);
addBooleanIfTrue(argv, CommandLineArgs.GENERATE_TESTRESULT_ATTRIBUTES, m_generateTestResultAttributes);
addReporterConfigs(argv);
addIntegerIfNotNull(argv, CommandLineArgs.SUITE_THREAD_POOL_SIZE, m_suiteThreadPoolSize);
addStringIfNotNull(argv, CommandLineArgs.XML_PATH_IN_JAR, m_xmlPathInJar);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/testng/internal/ExitCodeListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testng.internal;

import org.testng.IReporter;
import org.testng.IAttributes;
import org.testng.IReporter2;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
Expand All @@ -10,7 +11,7 @@

import java.util.List;

public class ExitCodeListener implements ITestListener, IReporter {
public class ExitCodeListener implements ITestListener, IReporter2 {
private boolean hasTests = false;
private final ExitCode status = new ExitCode();

Expand All @@ -23,7 +24,7 @@ public boolean hasTests() {
}

@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, IAttributes attributes) {
for (ISuite suite : suites) {
for (ISuiteResult suiteResult : suite.getResults().values()) {
ITestContext context = suiteResult.getTestContext();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/testng/reporters/EmailableReporter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.testng.reporters;

import org.testng.IAttributes;
import org.testng.IInvokedMethod;
import org.testng.IReporter;
import org.testng.IReporter2;
import org.testng.IResultMap;
import org.testng.ISuite;
import org.testng.ISuiteResult;
Expand Down Expand Up @@ -36,7 +37,7 @@
*
* @since 5.2
*/
public class EmailableReporter implements IReporter {
public class EmailableReporter implements IReporter2 {
private static final Logger L = Logger.getLogger(EmailableReporter.class);

// ~ Instance fields ------------------------------------------------------
Expand Down Expand Up @@ -65,9 +66,9 @@ public void setFileName(String fileName) {

/** Creates summary of the run */
@Override
public void generateReport(List<XmlSuite> xml, List<ISuite> suites, String outdir) {
public void generateReport(List<XmlSuite> xml, List<ISuite> suites, IAttributes attributes) {
try {
m_out = createWriter(outdir);
m_out = createWriter((String)attributes.getAttribute("defaultOutputDirectory"));
}
catch (IOException e) {
L.error("output file", e);
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/testng/reporters/EmailableReporter2.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.testng.reporters;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
Expand All @@ -12,7 +10,8 @@
import java.util.List;
import java.util.Set;

import org.testng.IReporter;
import org.testng.IAttributes;
import org.testng.IReporter2;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
Expand All @@ -29,7 +28,7 @@
/**
* Reporter that generates a single-page HTML report of the test results.
*/
public class EmailableReporter2 implements IReporter {
public class EmailableReporter2 implements IReporter2 {
private static final Logger LOG = Logger.getLogger(EmailableReporter2.class);

protected PrintWriter writer;
Expand All @@ -52,9 +51,9 @@ public String getFileName() {
}

@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, IAttributes attributes) {
try {
writer = createWriter(outputDirectory);
writer = createWriter((String)attributes.getAttribute("defaultOutputDirectory"));
} catch (IOException e) {
LOG.error("Unable to create output file", e);
return;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/testng/reporters/FailedReporter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testng.reporters;

import org.testng.IReporter;
import org.testng.IAttributes;
import org.testng.IReporter2;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestClass;
Expand Down Expand Up @@ -32,7 +33,7 @@
* @author <a href="mailto:[email protected]">Cedric Beust</a>
* @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
*/
public class FailedReporter extends TestListenerAdapter implements IReporter {
public class FailedReporter extends TestListenerAdapter implements IReporter2 {
public static final String TESTNG_FAILED_XML = "testng-failed.xml";

private XmlSuite m_xmlSuite;
Expand All @@ -45,9 +46,9 @@ public FailedReporter(XmlSuite xmlSuite) {
}

@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, IAttributes attributes) {
for (ISuite suite : suites) {
generateFailureSuite(suite.getXmlSuite(), suite, outputDirectory);
generateFailureSuite(suite.getXmlSuite(), suite, (String)attributes.getAttribute("defaultOutputDirectory"));
}
}

Expand Down Expand Up @@ -224,5 +225,4 @@ private static Map<String, String> findMethodLocalParameters(XmlTest srcXmlTest,

return Collections.emptyMap();
}

}
9 changes: 5 additions & 4 deletions src/main/java/org/testng/reporters/JUnitReportReporter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.testng.reporters;

import org.testng.IReporter;
import org.testng.IReporter2;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.IAttributes;
import org.testng.collections.ListMultiMap;
import org.testng.collections.SetMultiMap;
import org.testng.collections.Lists;
Expand All @@ -31,11 +32,11 @@
import java.util.Set;


public class JUnitReportReporter implements IReporter {
public class JUnitReportReporter implements IReporter2 {

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

Map<Class<?>, Set<ITestResult>> results = Maps.newHashMap();
ListMultiMap<Object, ITestResult> befores = Maps.newListMultiMap();
Expand Down Expand Up @@ -170,7 +171,7 @@ public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
}
xsb.pop(XMLConstants.TESTSUITE);

String outputDirectory = defaultOutputDirectory + File.separator + "junitreports";
String outputDirectory = attributes.getAttribute("defaultOutputDirectory") + File.separator + "junitreports";
Utils.writeUtf8File(outputDirectory, getFileName(cls), xsb.toXML());
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/testng/reporters/JqReporter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testng.reporters;

import org.testng.IReporter;
import org.testng.IAttributes;
import org.testng.IReporter2;
import org.testng.IResultMap;
import org.testng.ISuite;
import org.testng.ISuiteResult;
Expand All @@ -20,7 +21,7 @@
* @deprecated Use {@link org.testng.reporters.jq.Main} instead
*/
@Deprecated
public class JqReporter implements IReporter {
public class JqReporter implements IReporter2 {
private static final String C = "class";
private static final String D = "div";
private static final String S = "span";
Expand All @@ -35,8 +36,8 @@ public JqReporter() {

@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
String outputDirectory) {
m_outputDirectory = outputDirectory;
IAttributes attributes) {
m_outputDirectory = (String)attributes.getAttribute("defaultOutputDirectory");

XMLStringBuffer xsb = new XMLStringBuffer(" ");
xsb.push(D, "id", "suites");
Expand Down
Loading