How to skip TestNG test at runtime?


TestNG supports multiple ways to skip or ignore a @Test execution. Based on requirement, the user can skip a complete test without executing it at all or skip a test based on a specific condition. If the condition meets at the time of execution, it skips the remaining code in the test.

One can use the following ways to skip a @Test execution −

  • Use the parameter enabled=false at @Test. By default, this parameter is set as True.

  • Use throw new SkipException(String message) to skip a test.

  • Conditional Skip − User can have a condition check. If the condition is met, it will throw a SkipException and skip the rest of the code.

In this article, we will illustrate how to skip a test in a class using the above three ways.

Approach/Algorithm to solve this problem

  • Setp 1 − Create a TestNG class, NewTestngClass

  • Setp 2 − Write three different @Test methods in the class NewTestngClass, as shown in the programming code section.

    1st @Test Method It is enabled=false. It won’t execute at all and TestNG ignores it completely at runtime. Even in consolidated run details, it doesn’t consider it, so only two tests will be executed.

    2nd @Test Method It throws SkipException. TestNG will print the first line of code and skip the rest as soon as it reaches to SkipExecution code.

    3rd @Test Method

    It is conditional skip. The code checks whether the DataAvailable parameter is True or False. If it is False, it throws the SkipException and skips the test. But, if DataAvailable is True, it won’t throw the SkipException and continue the execution.
  • Step 3 − Create the testNG.xml as given below to run the TestNG classes.

  • Step 4 − Finally, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

Use the following code for the common TestNG class, NewTestngClass

src/ NewTestngClass.java

import org.testng.SkipException;
import org.testng.annotations.Test;
public class NewTestngClass {
   @Test(enabled=false)
   public void testcase1(){
      System.out.println("Testcase 1 - Not executed");
   }
   @Test
   public void testcase2(){
      System.out.println("Testcase 2 - skip exception example");
      throw new SkipException("Skipping this exception");
   }
   @Test
   public void testcase3(){
      boolean DataAvailable=false;
      System.out.println("Test Case3 - Conditional Skip");
      if(!DataAvailable)
      throw new SkipException("Skipping this exception");
      System.out.println("Executed Successfully");
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

Testcase 2 - skip exception example
Test ignored.
Test Case3 - Conditional Skip
Test ignored.
===============================================
Suite1
Total tests run: 2, Passes: 0, Failures: 0, Skips: 2
===============================================

Updated on: 12-Jan-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements