以编程方式跳过TestNG中的配置方法

时间:2014-03-11 11:23:03

标签: java testing testng

我有一个(希望)简单的问题 - 有没有办法在监听器中跳过配置方法?我有这样的代码

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");
                }
            }
        }
    }
}

所以,这当前正在工作,但是它也会跳过在跳过@BeforeMethod(groups = {"loginMethod"})之后应该执行的所有测试,但是我只需要跳过配置方法。那么如何实现我想要的任何方式呢?

1 个答案:

答案 0 :(得分:2)

您应该让听众实施IConfigurable而不是IInvokedMethodListener

这样的事情将跳过运行配置方法而不改变其状态:

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);
    }
}