Question

I have a (hopefully) easy question - is there a way how to skip configuration method in a listener?I have a code like this

public class SkipLoginMethodListener implements IInvokedMethodListener {

    private static final String SKIP_GROUP = "loginMethod";

    @Override
    public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult testResult) {
        ITestNGMethod method = invokedMethod.getTestMethod();
        if (method.isAfterMethodConfiguration() || method.isBeforeMethodConfiguration()) {
            for (String group : method.getGroups()) {
                if (group.equals(SKIP_GROUP)) {
                    System.out.println("skipped " + method.getMethodName());
                    throw new SkipException("Configuration of the method " + method.getMethodName() + " skipped");
                }
            }
        }
    }
}

So, this is currently working, however it will also skip all the tests which are supposed to be executed after the skipped @BeforeMethod(groups = {"loginMethod"}) but I need to skip only the configuration method. So is there a any way how to achieve what I want?

Was it helpful?

Solution

You should make your listener implement IConfigurable instead of IInvokedMethodListener.

Something like this will skip running the configuration method without changing its status:

public class SkipLoginMethodListener implements IConfigurable {
    private static final String SKIP_GROUP = "loginMethod";

    @Override
    public void run(IConfigureCallBack callBack, ITestResult testResult) {
        ITestNGMethod method = testResult.getMethod();
        if (method.isAfterMethodConfiguration() || method.isBeforeMethodConfiguration()) {
            for (String group : method.getGroups()) {
                if (group.equals(SKIP_GROUP)) {
                    System.out.println("skipped " + method.getMethodName());
                    return;
                }
            }
        }

        callBack.runConfigurationMethod(testResult);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top