有条件地跳过TestNG测试

use*_*021 6 java testing testng skip

我对TestNG注释没有多少经验,但我正在尝试使用TestNG框架和零售网站的POM设计模式构建测试套件.我打算使用数据驱动的方法.我的计划是通过excel驱动我的测试场景,而不是使用testng.xml.

例如,我将拥有多个测试套件,它们只是包名称TestSuite下的各种类文件.TestSuite名称将列在excel中,用户可以通过将运行模式更改为TRUE/FALSE来设置测试套件的运行模式.在这里,我计划实现一个条件检查,看看Run模式是否为FALSE,并相应地跳过testsuite,即testsuite类.

我们是否有任何直接的方法来使用TestNG来实现相同的目的,或者请用一个小例子来建议任何解决方案.

小智 14

请在测试开始时添加以下代码行,这将跳过您的测试用例

throw new SkipException("Skipping the test case");
Run Code Online (Sandbox Code Playgroud)


Mar*_*mro 7

您可以使用TestNG的注释转换器注释disabled属性设置@Test为true或false.

例:

public class MyTransformer implements IAnnotationTransformer {
    public void transform(ITest annotation, Class testClass,
        Constructor testConstructor, Method testMethod){

        if (isTestDisabled(testMethod.getName()))) {
            annotation.setEnabled(false);
        }
    }

    public boolean isTestDisabled(String testName){
       // Do whatever you like here to determine if test is disabled or not
    }
}
Run Code Online (Sandbox Code Playgroud)