9

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.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
user3000021
  • 288
  • 2
  • 3
  • 12

5 Answers5

14

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");
acutesoftware
  • 1,091
  • 3
  • 14
  • 33
user3744480
  • 149
  • 1
  • 3
7

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
    }
}
soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • Thanks for the prompt response, but I think you are trying to skip the test here. However my motive is to skip the test class itself. Here is how my folder structure looks like: com.core > framework.java com.pages com.util testsuite > testsuite1.java > testsuite2.java > testsuite3.java > testsuite4.java – user3000021 Nov 16 '13 at 19:05
  • You get the annotation, class, constructor and testMethod as argument. You can use whichever you like/need to skip the test. Skipping entire class is the same as skipping all tests in that class. – Mariusz Jamro Nov 16 '13 at 19:07
  • Ok, I ll try the same. Could you please tell me if all these test classes must have any annotations or its self or for the methods. I mean do I need to make the class with @Test and also the methods. – user3000021 Nov 16 '13 at 19:16
  • Hi I tried this, but looks like I have to be depended on the xml file, however I need an option where I will run a loop through the excel where I have a list of testsuite(in this case - these are class files), and I will check if the testsuite is runnable by checking its run mode. After that I will pass that name to the method which skip or continue to the class. If the class is skipped then I ll perform the same process until I get a class name which has runmode == true and then control goes inside the class and runs all @Test methods, however the loop will be waiting at the same position. – user3000021 Nov 17 '13 at 14:22
  • 1
    Is there a way to skip entire test suite if my initial sanity fails @BeforeSuite method ? – Paresh Nov 20 '15 at 18:00
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.

Mike G.
  • 3,860
  • 3
  • 17
  • 18
-2
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());


            }

}
soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
Arpan Saini
  • 4,623
  • 1
  • 42
  • 50
  • 1
    if that can be achieve better than this way , i will more appreciate if anybody can come with solution instead of giving down vote to this. – Arpan Saini Jan 22 '17 at 06:25
-3

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

    }
}
Arpan Saini
  • 4,623
  • 1
  • 42
  • 50