1.4. Example of ITestListener

1.4. Example of ITestListener



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
  • onTestFailedButWithinSuccessPercentage is invoked each time a method fails but is within the success percentage requested.
  • onFinish is invoked after all the tests have run and all their Configuration methods have been called.
TestListenerExample is our test class. It has a @BeforeTest and an @AfterTest method. It has four test methods:
  1. testcase1() is expected to run fine
  2. testcase2() is expected to fail as it doesn’t throw the expected exception
  3. testcase3() receives a parameter but since we haven’t set a DataProvider, it is skipped
  4. testcase4() is invoked five times, of which, twice its going to fail. We have set the expected successPercentage to 50.
 
TestListenerExample:
package com.ItestListener;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestListenerExample {
@BeforeTest
public void beforeTest() {
System.out.println("in before test");
}
@Test
public void testcase1() {
System.out.println("testcase1 test method");
}
@Test(expectedExceptions=RuntimeException.class)
public void testcase2() {
System.out.println("testcase2 test method will fail");
}
@Test
public void testcase3(String a) {
System.out.println("testcase3 test method will skip as parameter a is not set");
}
@Test(successPercentage=50, invocationCount=5)
public void testcase4() {
i++;
System.out.println("testcase4 test method, invocation count: " + i);
if (i == 1 || i == 2) {
System.out.println("fail testcase4");
Assert.assertEquals(i, 10);
}
}
@AfterSuite
public void afterTest() {
System.out.println("after test");
}
private int i;
}
TestListener is our implementation class for ITestListener. Each callback method prints a message so that we know whether the method is called.
TestListener:
package com.ItestListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class TestListener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
System.out.println("on test start method " + getTestMethodName(result) + " start");
}
@Override
public void onTestSuccess(ITestResult result) {
System.out.println("on test sucess method " + getTestMethodName(result) + " success");
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("on test failure method " + getTestMethodName(result) + " failure");
}
@Override
public void onTestSkipped(ITestResult result) {
System.out.println("testskipped method " + getTestMethodName(result) + " skipped");
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
System.out.println("test " + getTestMethodName(result) + " failed but within success");
}
@Override
public void onStart(ITestContext context) {
System.out.println("on start of test " + context.getName());
}
@Override
public void onFinish(ITestContext context) {
System.out.println("finishing Test... " + context.getName());
}
private static String getTestMethodName(ITestResult result) {
return result.getMethod().getConstructorOrMethod().getName();
}
}
testListenerTestng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestListenerExample Suite">
<listeners>
<listener class-name="com.ItestListener.TestListener" />
</listeners>
<test name="TestListenerExample">
<classes>
<class name="com.ItestListener.TestListenerExample" />
</classes>
</test>
</suite>
onStart is invoked first.
onTestStart is called once for each test before it is invoked.
onTestSuccess is invoked whenever a test passes. In our example, testcase1 always passes whereas, testcase4 passes three times.
onTestFailure is called for testcase2 as testcase2 will always fail. It is also called for testcase4 as it fails twice out of five times that it is invoked.
onTestSkipped is called once for testcaes3 as it is bound to skip.
onTestFailedButWithinSuccessPercentage is called once for testcase4, the first time it fails. It is not called again as it doesn’t match the requested successPercentage of 50
Finally onFinish is called once when the tests are all run.
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNG] Running:
on start of test TestListenerExample
in before test
on test start method t4 start
t4 test method, invocation count: 1
fail t4
test t4 failed but within success
on test start method t4 start
t4 test method, invocation count: 2
fail t4
test t4 failed but within success
on test start method t4 start
t4 test method, invocation count: 3
on test sucess method t4 success
on test start method t4 start
t4 test method, invocation count: 4
on test sucess method t4 success
on test start method t4 start
t4 test method, invocation count: 5
on test sucess method t4 success
on test start method testcase1 start
testcase1 test method
on test sucess method testcase1 success
on test start method testcase2 start
testcase2 test method will fail
on test failure method testcase2 failure
testskipped method testcase3 skipped
finishing Test... TestListenerExample
after test
===============================================
TestListenerExample Suite
Total tests run: 8, Failures: 3, Skips: 1
===============================================
view raw 4.Output.txt hosted with ❤ by GitHub

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.