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

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"})之后应该执行的测试,但是我只需要跳过配置方法。那么有什么方法可以实现我想要的吗?

最佳答案

您应该使监听器实现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);
    }
}

关于java - 以编程方式跳过TestNG中的配置方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22324275/

10-16 21:08