Skip Test in TestNG

08 December, 2014
With TestNG, we can skip tests in multiple ways based on our requirement. We can skip the complete test without executing it or we can skip the test when a specific condition is not satisfied.

In TestNG, @Test(enabled=false) annotation is used to skip a test case if it is not ready to test. We don't need to import any additional statements.


As in JUnit, TestNG will not show you the other test method as Skipped or Ignored. It will not consider that case method at all when the annotation is mentioned as “@Test(enabled=false)”

And We can Skip a test by using TestNG Skip Exception if we want to Skip a particular Test.
Syntax:

 
throw new SkipException("message"); 

And we can also perform a CONDITIONAL Skip, i.e.. We can have a condition to check and then SKIP test if condition is not satisfied. For Example, if there is no data available to perform a test, we can skip the test instead of making that test as failed.

Lets see all the above three cases by taking three simple tests.

Sample program.
 
import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipExample {
 
 @Test(enabled=false)
 public void testCaseEnabling(){
   System.out.println("I'm Not Ready, please don't execute me");
  }
 @Test
 public void testCaseSkipException(){
   System.out.println("Im in skip exception");
   throw new SkipException("Skipping this exception");
  }

 @Test
 public void testCaseConditionalSkipException(){
  System.out.println("Im in Conditional Skip");
  if(!DataAvailable)
  throw new SkipException("Skipping this exception");
  System.out.println("Executed Successfully");
 }

}
Once i execute the above program the output should of the program should look as below . Test testCaseConditionalSkipException will be skipped only when data is not available



No comments:

Post a Comment