Ignore a Test / Skip a test in TestNG

In this section, we will discuss how to ignore a Test / skip a Test in TestNG. Ignoring or Skip a test is so important when you will be creating big framework. There is various reason to ignore a test. Sometimes our code is not completely ready to execute which leads the Test case failed. Sometimes we need to skip of ignore the test to run the other test. In this case we need to use @Test(enable = False). Enabled attribute helps to enable and disable base the value you specify.  Once you specify the @Test as disable then this test will not be executed and bypassed to nest test.

Example :

There are several ways to perform ignore the Test in TestNG based on requirement. Below are the some of them

  • Skipping the complete Test without executing
  • Skip a particular test by using skip exception
  • Skip a test based on condition

Above are 3 scenario where we can skip test in TestNG. Let’s discuss in detail.

1. Skip the complete Test without executing

This is the simple scenario when we want to skip the complete test without sending any message. When our Test is not ready also we can use this scenario to skip the Test.

If we pass the enable = false to @Test as parameter, then this will ignore the Test completely.

Syntax:   @Test(enabled=false) 

Example :

Package testinglpoint;

import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipTestExample {
	@Test(enabled=false)
	public void testCaseEnable(){
			System.out.println("Test is not ready. Execution  not required.");
		}
}

2. Skip a particular test by using skip exception

This is the advance scenario of the Scenario-1. If we want to skip the test and sending message to console. In this we will throw SkipException() exception to return the message to console.

throw new SkipException("Message for console");

Example :

Package testinglpoint;

import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipTestExample {
	@Test
	public void testCaseSkipException(){
			System.out.println("Test is not ready. Execution  not required ");
			throw new SkipException("Skipping with exception");
		}
}

3. Skip a test based on condition

If we want to skip the particular Test under some special condition, we can use this scenario. In this scenario, we are using the if statement to validate the condition. If condition is satisfied, then the Test will be skipped otherwise Test will not be skipped.

For example, if there are no data available or no database connection available, then can skip the Test instead making that Test failed.

Example :

Package testinglpoint;

import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipTestExample {
	@Test
	public void testCaseConditionalSkipException(){
		System.out.println("Conditional Skip");
		if(!DataAvailable)
		throw new SkipException("Skipping this exception");
		System.out.println("Executed Successfully");
	}
}

Leave a Reply

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