How to skip test cases in TestNG ?

Skip Test Cases during execution in TestNG

Sometimes you may need to skip some test cases from your test suite. so the best way to do it in testNG is by making  that test case enable is equal to false.

 we can easily enable or disable any test case in TestNG.

@Test(enabled=false)

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.

Please go through the below sample program

package Skip;

import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipExample {

@Test(enabled=false)
public void testCaseEnabling(){
System.out.println("I will not get printed");
}
@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");
}

}

@ignore annotation in TestNG,

When used at the method level @Ignore annotation is functionally equivalent to @Test(enabled=false). Here’s a sample that shows how to ignore all tests within a class.

import org.testng.annotations.Ignore;
import org.testng.annotations.Test;

@Ignore
public class TestcaseSample {

@Test
public void testMethod1() {
}

@Test
public void testMethod2() {
}
}

In below screenshot you can see the highlighted part a test case has been disabled with the help of enabled keyword

disable test cases in testng

So we have seen there are two method with which we can ignore the test cases in testNG.

If you are a Selenium tester you may be asked testNG questions  in the interview room. Best way to prepare it in advance for TestNG interview questions please click on this link

so please let me know your thoughts below in the comment section

Author: Saad Mansuri

Leave a Reply

Your email address will not be published. Required fields are marked *