Road to override TestNg listener methods

In this post I will show you how to create custom testng listener  class and override its methods.
Create java class like I created below “MyListner” and inherit “TestListenerAdapter” testing class.

package com.test;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class MyListner extends TestListenerAdapter {
       
     @Override
     public void onTestFailure(ITestResult result) {
        System.out.println(result.getName() + " Test method failed\n");
     }

     @Override
     public void onTestSkipped(ITestResult result) {
        System.out.println(result.getName() + " Test method skipped\n");
     }

     @Override
     public void onTestSuccess(ITestResult result) {
        System.out.println(result.getName() + " Test method success\n");
     } 
 
Create testng class and use above listener like below
package com.test;

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

@Listeners ({MyListner.class})
public class SampleTest {

    @Test
    public void testFirst() {
        Assert.assertTrue(true);
    }

    @Test
    public void testSecond() {
        Assert.assertTrue(false);
    }

    @Test(dependsOnMethods = { "testSecond" })
    public void testThird() {
        Assert.assertTrue(true);
    }
}

Run above test you will see below message in console:


2 comments:

  1. Hi,
    Your posts are really helpful..
    Can you help me in auto running failed test's in Testng..

    ReplyDelete
    Replies
    1. You can check testng-failed.xml file after your execution in test-output folder if it exist, then try to run them alone. It only contains test scripts those are failed in your current execution activities. there are several ways to fix this issue other than this. But its quick way to do. Hope it helps you.

      Delete

Leave your comments, queries, suggestion I will try to provide solution