logo

The best IT Trainig Institute In Gurgaon

Skip Test Cases in TestNG

SkipException in TestNG is an exception that you can use to skip a test case dynamically during its execution. When you throw a SkipException, TestNG immediately halts the execution of that test and marks it as "skipped" in the test report.

package asc;

import org.testng.SkipException;
import org.testng.annotations.Test;

public class SkipTest {
    
    
    Boolean datasetup = true;
    
    
    @Test(enabled=false)
    public void skipTest1() {
        System.out.println("Skipping this test as it is not complete");
    }
    
    
    @Test
    public void skipTest2() {
        System.out.println("Skipping this test forcefully");
        throw new SkipException("Skipping this test");
    }
    
    
    @Test
    public void skipTest3() {
        System.out.println("Skipping this test baseed on condition");
        if(datasetup) {
            System.out.println("Execute the test");
        }
        else
        {
            System.out.println("Do not execute the further steps");
            throw new SkipException("Do not execute the further steps");
        }
    }
    

}
When to Use SkipException:
    1. Conditional Test Execution:You might have test cases that should only run if certain conditions are met. For example, a test that depends on external resources or configurations that may not always be available.
    2. Incomplete Features: Sometimes, a test might be written for a feature that's not fully implemented or is temporarily unavailable. Instead of failing the test, you can skip it until the feature is ready.
    3. External Dependencies:If a test depends on an external system (like a database, a web service, or a file), and that system is unavailable, you might want to skip the test rather than let it fail.
How SkipException Works:
    1. When you throw a SkipException in a test method, TestNG catches it and skips the remaining part of the test.
    2. The test is then marked as "skipped" in the test results, and the message passed to the SkipException constructor is recorded as the reason for skipping.
Example Explanation
@Test
public void skipTest2() {
    System.out.println("Skipping this test forcefully");
    throw new SkipException("Skipping this test");
}
                    
What Happens Here:
    1. Test Execution Begins: When TestNG starts executing the skipTest2 method, it first prints " Skipping this test forcefully " to the console.
    2. Throwing SkipException: The method then throws a new SkipException with the message " Skipping this test ".
    3. Skipping the Test: Upon encountering the SkipException, TestNG stops the execution of the skipTest2 method and marks the test as "skipped."
    4. Test Report: In the TestNG report, this test will appear under the "Skipped" section, and the message "Skipping this test" will be shown as the reason for skipping.
Practical Example of Using SkipException

Consider a scenario where you have a test that should only run if a specific configuration file is present:

@Test
public void testWithExternalConfig() {
    File configFile = new File("config.properties");
    if (!configFile.exists()) {
        throw new SkipException("Skipping test as config file is missing");
    }
    // Continue with the test if the file exists
    System.out.println("Config file found, proceeding with the test");
}
                        
Explanation:
  • Before running the actual test, the code checks whether the config .properties file exists.
  • If the file does not exist, the test is skipped by throwing a SkipException with a meaningful message explaining why the test was skipped.
  • If the file does exist, the test proceeds as normal.
Benefits
  • Clean Test Reports: By using SkipException, you avoid cluttering your test reports with "failed" tests that weren't meant to run under certain conditions.
  • Flexible Testing: It allows you to write more flexible and robust test suites that can handle varying environments and dependencies gracefully.
Let's Break the Code
1. Package and Imports
package asc;

import org.testng.SkipException;
import org.testng.annotations.Test;
                            
    1. package asc;
      defines the package to which this class belongs.
    2. import org.testng.SkipException;
      imports the SkipException class from the TestNG framework. This exception is used to skip a test during runtime.
    3. import org.testng.annotations.Test;
      imports the Test annotation, which is used to mark methods as test cases in TestNG.
2. Class Defination
public class SkipTest {
Boolean datasetup = true;
                        
    1. public class SkipTest { ... }
      defines a public class named SkipTest.
    2. Boolean datasetup = true;
      is a boolean variable that is used later in the code to conditionally skip a test based on its value.
3. Test Method 1: skipTest1
@Test(enabled=false)
 public void skipTest1() {
     System.out.println("Skipping this test as it is not complete");
 }
                            
    1. @Test(enabled=false)
      is a TestNG annotation that marks the method as a test case but disables it from being executed.
    2. This method will not run because enabled = false is set, meaning the test is explicitly skipped due to being incomplete or not ready for execution.
4. Test Method 2: skipTest2
@Test
public void skipTest2() {
    System.out.println("Skipping this test forcefully");
    throw new SkipException("Skipping this test");
}
                            
    1. @Test marks this method as a test case.
    2. This method prints a message to the console and then throws a SkipException. Throwing this exception during the test execution causes TestNG to skip this test and report it as "skipped" in the test results.
    3. The message " Skipping this test " is passed as a parameter to the SkipException, which can help identify the reason for skipping in the test report.
5. Test Method 3: skipTest3
@Test
public void skipTest3() {
    System.out.println("Skipping this test baseed on condition");
    if (datasetup) {
        System.out.println("Execute the test");
    } else {
        System.out.println("Do not execute the further steps");
        throw new SkipException("Do not execute the further steps");
    }
}
                                
    1. @Test marks this method as a test case.
    2. The method first prints a message to the console.
    3. It then checks the value of the datasetup boolean variable.
      • If datasetup is true, it prints " Execute the test " and proceeds with the test execution.
      • If datasetup is false, it prints " Do not execute the further steps " and throws a SkipException, which skips the remaining steps of the test.
Summary
  • skipTest1 is an example of a test that is skipped because it is marked as enabled = false.
  • skipTest2 demonstrates how to forcibly skip a test during its execution using SkipException.
  • skipTest3 shows how to conditionally skip a test based on the value of a variable. If the condition is not met, the test is skipped using SkipException.