The method that has enabled=false not getting printed using IClassListener in TestNG

Viewed 29

The Testcase code looks like below.

public class LaunchBrowser{
    
    @Test(enabled=false)
        public void browser() throws IOException, InterruptedException {
            System.out.println("Test 1");
    }
}

The CustomListener class looks like below:

public class ListenerImplements extends TestBase implements IClassListener {
    
    @Override
    public void onBeforeClass(ITestClass testClass) {
        ITestNGMethod[] methods = testClass.getTestMethods();

        for (ITestNGMethod method: methods) {
            System.out.println(method.getEnabled());
        }
    }

I am calling the Listener Class inside the testNg.xml file.

When I use the above code nothing gets printed. But when I use multiple test methods in a class with one being enabled and another one being disabled, I can see the console with disabled testcase as well.

The problem is when we have only one test method and that method is disabled using testng function enabled=false.

My requirement is to know whether the testcase is enabled/disabled.

1 Answers

Looks like the test class is omitted when all test method is disabled. What you can do is implement ISuiteListener instead, where getAllMethods() are enabled methods and getExcludedMethods() are disabled methods.

public class SuiteListener implements ISuiteListener {
    @Override
    public void onStart(ISuite suite) {
        suite.getAllMethods().forEach(
                m ->
                        System.out.println("%s %s %s".formatted(m.getTestClass(), m.getMethodName(), "enabled")));
        suite.getExcludedMethods().forEach(
                m ->
                        System.out.println("%s %s %s".formatted(m.getTestClass(), m.getMethodName(), "disabled")));
    }
}
Related