ITestListener


ITestListener is the listener for test running. You can either implement ITestListener or extend the TestNG provided implementation TestListenerAdapter.

ITestListener has methods on following events:

onStart: is invoked after the test class is instantiated and before any configuration method is called
onTestSuccess: is invoked on success of a test
onTestFailure: is invoked on failure of a test
onTestSkipped: is invoked whenever a test is skipped
onFinish: is invoked after all the tests have run and all their Configuration methods have been called.

Lets create a Listener class

package testngexp;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

publicclass TestListenersexp extends TestListenerAdapter {
privateintm_count = 0;
@Override
publicvoid onTestStart(ITestResult result) {
printlog("on test method " + result.getName() + " start ---");
}

@Override
publicvoid onTestFailure(ITestResult tr) {
log(tr.getName()+ "--Test method failed\n");
}

@Override
publicvoid onTestSkipped(ITestResult tr) {
log(tr.getName()+ "--Test method skipped\n");
}

@Override
publicvoid onTestSuccess(ITestResult tr) {
log(tr.getName()+ "--Test method success\n");
}

@Override
publicvoid onStart(ITestContext context) {
log("on start of test " + context.getName());
System.out.println("");
}

@Override
publicvoid onFinish(ITestContext context) {
log("on finish of test " + context.getName());
}

privatevoid printlog(String string) {
System.out.print(string);
if (++m_count % 40 == 0) {
System.out.println("");
}
}
}

Lets create another java class

package testngexp;
import org.testng.Assert;
import org.testng.annotations.Test;

publicclass listnertestcase {
@Test
publicvoid testMethodOne(){
Assert.assertTrue(true);
}

@Test
publicvoid testMethodTwo(){
Assert.assertTrue(false);
}

@Test(dependsOnMethods={"testMethodTwo"})
publicvoid testMethodThree(){
Assert.assertTrue(true);
}
}

TestNG.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
<suitename="Suite"verbose="1">
<listeners>
<listenerclass-name="testngexp.TestListenersexp"/>
</listeners>
<testname="Test1">
<classes>
<classname="testngexp.listnertestcase"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->

Results
image

Comments