Tuesday, 6 August 2013

Setting listener in testng.xml

1. Following is the implementation of sample testng listener :

public class MyListener implements ITestListener
{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish(ITestContext arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onStart(ITestContext arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onTestFailure(ITestResult arg0) {
        // TODO Auto-generated method stub
        arg0.getThrowable().printStackTrace();
    }

    @Override
    public void onTestSkipped(ITestResult arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onTestStart(ITestResult arg0) {
        // TODO Auto-generated method stub
       
    }

    @Override
    public void onTestSuccess(ITestResult arg0) {
        // TODO Auto-generated method stub
       
    }

}



2. Following is the sample tesng.xml file

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
 
<suite name="Example" verbose="1">
  <parameter name="a" value="cde"/>
 
  <test name="Simple example" >
    <classes>
      <class name="Test" />
    </classes>
  </test>
  <listeners>
      <listener class-name="MyMyListener "></listener>
  </listeners>
  </suite>


3. Following is the class file which is used in testng.xml file.

public class Test {
   
   
   
    public static void main(String []arg)
    {
        Test t=null;
        t.fun2();
       
       
       
    }
   
    static void fun2()
    {
       
        System.out.println("in fun...");
    }
   
   
    @org.testng.annotations.Test()
    public void fun()
    {
        Reporter.log("This is sample");
        System.out.println("in fun");
        Reporter.log("Test fun log1", 0);
        //Assert.assertEquals(true,false);
    }
   

    @org.testng.annotations.Test()
    public void fun1()
    {
        Reporter.log("This is sample 2");
        Reporter.log("Test fun log2", 1);
        System.out.println("in fun");
    }

}




1 comment:

  1. Hi,
    1. Is this the only use of listeners to get reports?
    2. What override annotation does?
    -
    Beginner in Automation testing

    ReplyDelete