Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional skipping of TestNG tests

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.

like image 513
user3000021 Avatar asked Nov 16 '13 18:11

user3000021


3 Answers

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");
like image 103
user3744480 Avatar answered Oct 23 '22 10:10

user3744480


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
    }
}
like image 36
Mariusz Jamro Avatar answered Oct 23 '22 10:10

Mariusz Jamro


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.

like image 30
Mike G. Avatar answered Oct 23 '22 09:10

Mike G.