Java 有条件地跳过 TestNG 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20022288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:24:19  来源:igfitidea点击:

Conditional skipping of TestNG tests

javatestingtestngskip

提问by user3000021

I don't have much experience with TestNG annotations, however I am trying to build a test suite using TestNG framework and POM design pattern for a retail website. I am planning to use a data driven approach. My plan is to drive my test scenarios through excel and not using testng.xml.

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

For example I will be having number of testsuites, which will be nothing but various class files under a package name TestSuite. TestSuite names will be listed in an excel and user will be allowed to set the run mode of the testsuite by changing the run mode to TRUE/FALSE. Here I am planning to implement a condition check to see if the Run mode is FALSE and accordingly skip the testsuite, i.e. the testsuite class.

例如,我将拥有许多测试套件,这些测试套件只不过是包名称 TestSuite 下的各种类文件。TestSuite 名称将在 excel 中列出,并且允许用户通过将运行模式更改为 TRUE/FALSE 来设置测试套件的运行模式。在这里,我计划实施条件检查,以查看 Run 模式是否为 FALSE 并相应地跳过测试套件,即测试套件类。

Do we have any direct method to achieve the same using TestNG or please suggest any solution for the same with a small example.

我们是否有任何直接的方法可以使用 TestNG 实现相同的目标,或者请通过一个小示例为相同的解决方案提出任何解决方案。

采纳答案by Mariusz Jamro

You can use TestNG's annotation transformerto set the disabledproperty of a @Testannotation to true or false.

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

Example:

例子:

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

回答by user3744480

Please add the below line of code in the beginning of the test which will skip your test case

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

throw new SkipException("Skipping the test case");

回答by Arpan Saini

throw new skipException("skipping the test case")

It will just skip the test case not the complete suite. Instead of checking the suite Run mode, check the Run mode of test case as Y/N in the @beforeTestMethod. Then if you found the run mode as N, throw an exception .

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

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

This will skip your test case. This is just an alternative even I didn't find any other way to skip the complete suite. The above case will fill the purpose just need to keep the run mode of each test case as N, if you don't want to run that suite. It will skip all the test cases of that suite and will be part of your report that these test cases were skipped.

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

Example is as given below

示例如下

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


            }

}

回答by Arpan Saini

The best solution I have found, We can check the run mode of the Suite in @BeforeTest Annotation method if it found N, Then throw new skip Exception, it will skip all the test case of that suite, I was doing a mistake to catch the Exception in try catch block that's why it was going to check all the test cases after throw skip exception.

我找到的最好的解决方案,如果找到N,我们可以在@BeforeTest Annotation方法中检查Suite的运行模式,然后抛出新的skip Exception,它将跳过该套件的所有测试用例,我犯了一个错误来捕捉try catch 块中的异常,这就是为什么它要在抛出跳过异常后检查所有测试用例。

Please find below the right example how to skip all the test cases in a suite, if suite run mode found as N in suite Excel. running this through testng.xml

如果套件运行模式在套件 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");     

    }
}

回答by Mykola G.

Since 6.13 throw new SkipException("Skipping the test case");won't work. I have to set status before throwing exception, otherwise test status will be failure instead of skipped:

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

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

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

Probably it will be fixed/changed in next releases, please see https://github.com/cbeust/testng/issues/1632for details.

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