Skip Exception Test in TestNG

Using TestNG, we have multiple ways to Skip a test based on our requirement. We can Skip complete test without executing it or we can Skip a 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.

Let us see all the above three cases by taking three simple tests.

Please find the below sample program.

package packOne;

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
skip test in TestNG

Test Frameworks: 

Comments

THanks!

Can i pass a Boolean variable value to the Enabled

Thanks a lot!

Thanks!

throw new SkipException("Skipping this exception");

Thanks for ur answer....

Thanks a lot, King of TestNG

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.