TestNG测试的条件跳过

9
我在TestNG注解方面没有太多经验,但我正在尝试使用TestNG框架和POM设计模式为零售网站构建测试套件。我计划采用数据驱动的方法。我的计划是通过Excel来驱动我的测试场景,而不是使用testng.xml。
例如,我将拥有许多测试套件,这些套件将仅是包名TestSuite下的各种类文件。 TestSuite名称将列在Excel中,并允许用户通过更改运行模式将测试套件的运行模式设置为TRUE / FALSE。 在这里,我计划实现条件检查,以查看运行模式是否为FALSE,并相应地跳过测试套件,即测试套件类。
我们是否有任何直接的方法可以使用TestNG实现相同的效果,或者请提供任何解决此问题的解决方案,并附带一个小示例。
5个回答

14
请在测试开始时添加以下代码行,这将跳过您的测试用例。
throw new SkipException("Skipping the test case");

7
你可以使用TestNG的注解转换器来将@Test注解的disabled属性设置为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
    }
}

感谢您的迅速回复,但我认为您试图跳过测试。然而,我的动机是要跳过测试类本身。这是我的文件夹结构:com.core > framework.java com.pages com.util testsuite > testsuite1.java > testsuite2.java > testsuite3.java > testsuite4.java - user3000021
你会得到注释、类、构造函数和测试方法作为参数。你可以使用任何你喜欢或需要的来跳过测试。跳过整个类就相当于跳过该类中的所有测试。 - Mariusz Jamro
好的,我会尝试相同的操作。请问所有这些测试类都必须有注释吗?是针对类本身还是方法?我的意思是,我需要在类和方法上都加上@Test注释吗? - user3000021
嗨,我尝试了这个,但是看起来我必须依赖于xml文件,然而我需要一个选项,在其中我将遍历excel,其中有测试套件列表(在这种情况下 - 这些是类文件),并且我将检查测试套件是否可运行通过检查其运行模式。之后,我将将该名称传递给跳过或继续到该类的方法。如果跳过该类,则执行相同的过程,直到获得runmode == true的类名,然后控制进入类并运行所有@Test方法,但是循环将在相同位置等待。 - user3000021
1
如果我的@BeforeSuite方法无法通过初始测试,有没有一种跳过整个测试套件的方法? - Paresh

3

自6.13版本起,throw new SkipException("Skipping the test case");不再起作用。在抛出异常之前,我必须先设置状态,否则测试状态将是失败而不是跳过:

iTestResult.setStatus(TestResult.SKIP); throw new SkipException("Skipping the test case");

可能会在下一个版本中修复/更改,请参见https://github.com/cbeust/testng/issues/1632了解详情。


-2
throw new skipException("skipping the test case")

它只会跳过测试用例而不是整个套件。不要检查套件运行模式,而是在@beforeTest方法中检查测试用例的运行模式是否为Y/N。然后,如果您发现运行模式为N,请抛出异常。

throw new skipException("skipping test case as run mode is y").

这将跳过您的测试用例。即使我没有找到其他跳过整个测试套件的方法,这只是一种替代方法。上述情况将达到目的,只需要将每个测试用例的运行模式保持为N,如果您不想运行该套件,则会跳过该套件的所有测试用例,并且这些测试用例将成为报告的一部分。

示例如下:

package com.qtpselenium.suiteC;

import org.testng.SkipException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.qtpselenium.util.TestUtil;

public class TestCaseC1 extends TestSuiteBase{


    //Checking runMode of the Test case in Suite
        @BeforeTest
        public void checkTestcaseskip(){

            //this.getclass().getSimpleName() method returns the name of the class
            App_Logs.debug("checking run mode of " +this.getClass().getSimpleName() + " testcase");
            if(!TestUtil.IsTestCaseRunnable(suiteCxlsx, this.getClass().getSimpleName())){
                App_Logs.debug("Run mode of testcase " + this.getClass().getSimpleName() + " is N");
                throw new SkipException("Run mode of testcase " + this.getClass().getSimpleName() + " is N");

            }else

            App_Logs.debug("Run mode of testcase " + this.getClass().getSimpleName() + " is Y");
        }

        @Test(dataProvider="getTestData")
        public void TestcaseC1(

                              String col1,
                              String col2,
                              String col3,
                              String col4

                             ){


            App_Logs.debug("Test data of testcase : " +  this.getClass().getSimpleName());
            App_Logs.debug(col1+"--"+col2+"--"+col3+"--"+col4);


    }

    //Data provide to TestcaseC1
            @DataProvider
            public Object[][] getTestData(){


                return TestUtil.getdata(suiteCxlsx, this.getClass().getSimpleName());


            }

}

1
如果有更好的解决方案,我会更感激任何人提供解决方案,而不是给这个问题投反对票。 - Arpan Saini

-3

我找到的最佳解决方案是,在@BeforeTest注释方法中检查Suite的运行模式,如果发现为N,则抛出skip Exception,它将跳过该套件的所有测试用例。我之前犯了一个错误,在try catch块中捕获异常,这就是为什么它在抛出skip exception后继续检查所有测试用例的原因。

请参考以下正确示例,如何在suite Excel中找到运行模式为N时跳过所有测试用例,通过testng.xml运行此示例。

package com.qtpselenium.suiteC;

import org.testng.SkipException;
import org.testng.annotations.BeforeSuite;

import com.qtpselenium.base.TestBase;
import com.qtpselenium.util.TestUtil;

public class TestSuiteBase extends TestBase{


    @BeforeSuite
    public void checksuiteskip(){

        try {
            //Initialize method of Test BASE Class to Initialize the logs and all the excel files
            Initialize();

        } catch (Exception e) {

            e.printStackTrace();
        }

             App_Logs.debug("checking run mode of SuiteC");
            if( !TestUtil.isSuiterunnable(suitexlsx, "suiteC")){

               App_Logs.debug("Run mode for suiteC is N");
               throw new SkipException("Run mode for suiiteC is N");

              }else

                   App_Logs.debug("Run mode for SuiteC is Y");     

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接