forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable customization of testng-results.xml
Closes testng-team#2171 In order for one to be able to generate their own customized version of testng-results.xml without a lot of code duplication, the following should be done. 1. Extend org.testng.reporters.XMLReporter 2. Override “fileName()” method and provide your own file name. 3. Override “addCustomTagsFor()” method and plugin your implementation which will add one or more tags to the xml file. 4. Add the newly created class as one of the listeners via “@listeners” annotation or via “<listeners>” tag or via SPI approach. For a sample of how the customized listener can look like, take a look at the sample that resides in “src/test/java/test/reports/issue2171/MyExampleListener.java”
- Loading branch information
1 parent
194eb64
commit 993c8c9
Showing
7 changed files
with
89 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/org/testng/reporters/ICustomizeXmlReport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.testng.reporters; | ||
|
||
import org.testng.ITestResult; | ||
|
||
/** | ||
* An interface that helps add custom xml tags to the TestNG generated xml report. | ||
*/ | ||
public interface ICustomizeXmlReport { | ||
|
||
/** | ||
* @param xmlBuffer - An {@link XMLStringBuffer} object that represents the buffer to be used. | ||
* @param testResult - An {@link ITestResult} object that represents a test method's result. | ||
*/ | ||
void addCustomTagsFor(XMLStringBuffer xmlBuffer, ITestResult testResult); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/test/java/test/reports/issue2171/MyExampleListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package test.reports.issue2171; | ||
|
||
import java.util.Properties; | ||
import org.testng.ITestResult; | ||
import org.testng.reporters.XMLReporter; | ||
import org.testng.reporters.XMLStringBuffer; | ||
|
||
public class MyExampleListener extends XMLReporter { | ||
|
||
@Override | ||
public String fileName() { | ||
return "issue_2171.xml"; | ||
} | ||
|
||
@Override | ||
public void addCustomTagsFor(XMLStringBuffer xmlBuffer, ITestResult testResult) { | ||
Properties props = new Properties(); | ||
for (String attributeName : testResult.getAttributeNames()) { | ||
props.setProperty("path", testResult.getAttribute(attributeName).toString()); | ||
xmlBuffer.push(attributeName, props); | ||
xmlBuffer.pop(attributeName); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/test/reports/issue2171/TestClassExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package test.reports.issue2171; | ||
|
||
import org.testng.ITestResult; | ||
import org.testng.Reporter; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
|
||
@Listeners(MyExampleListener.class) | ||
public class TestClassExample { | ||
|
||
@Test | ||
public void testMethod() { | ||
ITestResult result = Reporter.getCurrentTestResult(); | ||
result.setAttribute("file", "issue2171.html"); | ||
} | ||
|
||
} |