-->

Conditional skipping of TestNG tests

2019-03-28 11:03发布

问题:

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.

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.

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

回答1:

You can use TestNG's annotation transformer to set the disabled property of a @Test annotation to true or 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
    }
}


回答2:

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


回答3:

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:

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/1632 for details.



回答4:

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 @beforeTest Method. Then if you found the run mode as N, throw an exception .

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.

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


            }

}


回答5:

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.

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

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

    }
}