testng中java编程跳过配置方法

weylhg0b  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(410)

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

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"}) 但是我只需要跳过配置方法。那么,有没有办法实现我想要的呢?

neekobn8

neekobn81#

您应该让侦听器实现IConfiguration,而不是 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);
    }
}

相关问题