Reporters: These classes are called when the whole suite run ends.
Listeners And Reporters
//1 . sample test script
public class Sample{
@Test
public void testMethodOne(){
Assert.assertTrue(true);
}
@Test
public void testMethodTwo(){
Assert.assertTrue(false);
@Test(dependsOnMethods={"testMethodTwo"})
public void testMethodThree(){
Assert.assertTrue(true);
}
}
}
//2. Listener Class
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class CustomListener extends TestListenerAdapter{
private int m_count = 0;
@Override
public void onTestFailure(ITestResult tr) {
log(tr.getName()+ "--Test method failed\n");
}
@Override
public void onTestSkipped(ITestResult tr) {
log(tr.getName()+ "--Test method skipped\n");
}
@Override
public void onTestSuccess(ITestResult tr) {
log(tr.getName()+ "--Test method success\n");
}
private void log(String string) {
System.out.print(string);
if (++m_count % 40 == 0) {
System.out.println("");
}
}
}
//3.IReporter
public class CustomReporter implements IReporter{
@Override
public void generateReport(List xmlSuites, List suites,
String outputDirectory) {
//Iterating over each suite included in the test
for (ISuite suite : suites) {
//Following code gets the suite name
String suiteName = suite.getName();
//Getting the results for the said suite
Map suiteResults = suite.getResults();
for (ISuiteResult sr : suiteResults.values()) {
ITestContext tc = sr.getTestContext();
System.out.println("Passed tests for suite '" + suiteName +
"' is:" + tc.getPassedTests().getAllResults().size());
System.out.println("Failed tests for suite '" + suiteName +
"' is:" +
tc.getFailedTests().getAllResults().size());
System.out.println("Skipped tests for suite '" + suiteName +
"' is:" +
tc.getSkippedTests().getAllResults().size());
}
}
}
}
//4. testng.xml
<suite name="some Listener">
<listeners>
<listener class-name="CustomListener" />
<listener class-name="CustomReporter" />
</listeners>
<test name="some name">
<classes>
<class name="Sample" />
</classes>
</test>
</suite>
//--------------Output--------------------------
testMethodOne--Test method success
testMethodTwo--Test method failed
testMethodThree--Test method skipped
===============================================
Simple Logger Suite
Total tests run: 3, Failures: 1, Skips: 1
===============================================
Passed tests for suite 'Simple Reporter Suite' is:1
Failed tests for suite 'Simple Reporter Suite' is:1
Skipped tests for suite 'Simple Reporter Suite' is:1
No comments:
Post a Comment