Skip / Disable / Ignore Tests in TestNG:

 

  • Sometimes we can face a situation where our test cases can not be prepared and we have to stop those tests from running due to exception issue’s.

 

Ways to Skip / Disable / Ignore Tests in TestNG:

We can achieve this requirement in three ways:

  1. Making parameter “enabled” as “false”.
  2. Using “exclude” parameter in testng.xml.
  3. “throw new SkipException()” in the if condition to Skip / Ignore Test.

 

1. Making parameter “enabled” as false:

 

  • Using @Test(enabled = false) this method. We are able to skip / ignore method.
  • Using @Test(enabled = false) method we can not run the method it simply skip / ignore method and execute next one.
  • If we want to execute / run method then simply add true above the method e.g: @Test(enabled = true) .

 

Example: Skip / Ignore method using @Test(enabled = false)

  • Right click on the testng.xml and run it as ‘TestNG Suite’ to execute script.

Output:

 

2. Using “exclude” parameter in testng.xml:

 

  • TestNg provides an option to include or exclude for Groups, Test Methods, Classes and Packages using include and exclude tags by defining in testng.xml.
  • Only include methods will be Execute / Run other methods will be skip / ignore.

 

  • Using <exclude name=”method_name” /> this method. We are able to skip / ignore method. 

 

  • Using < include name=”method_name” /> this method. Execute method properly.

 

  • We will create a Class with three Test Methods. In that we will include two test methods and try to exclude one test method.

Example: class file with three test methods.

Example: Use Exclude / Include in TestNG

Output:

 

3. skipping a test method is by using throw new SkipException() exception:

 

  • Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.

 

Example: Skip / Ignore Test in TestNG using SkipException()

Output:

How to skip or ignore execution of tests in TestNG and TestNG Ignore or Disable Tests and Skip Exception Test in TestNG