Open In App

How to skip TestNG test at runtime?

Last Updated : 24 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

With TestNG, there are situations where some tests are needed to be skipped during runtime based on certain conditions. Dynamic skipping of tests provides more flexibility while executing the test at run-time, depending upon some external factor like configuration, system states, or the outcome of some other tests. TestNG provides a easy mechanism for skipping a test at runtime through its SkipException.

Approach to skip TestNG test at runtime

To implement a solution where tests can be skipped dynamically in TestNG, follow these generalized steps:

  • Set Up the TestNG Environment: It is important to make sure that test-skipping is properly set up in your project. This includes including the TestNG library within your project's dependencies, such as using Maven or JARs, and creating test classes.
  • Create a Java Class for Tests: Create a new Java class containing test methods. This class will contain both conditional and direct test-skipping examples.
  • Write a Directly Skipped Test: In one of your test methods, use SkipException to directly skip the test. No condition needs to be checked here—just log that the test is skipped and throw SkipException.
  • Write a Conditionally Skipped Test: In another testing methodology, use logic that checks whether the test should run or be skipped. It might be checking for missing data, failed API responses, configuration problems, or perhaps any runtime conditions. In case this condition is satisfied, a SkipException must be thrown and this test skipped.
  • Compile and Execute Tests: Compile the test class and run it using TestNG.

Example Code

Here is the code that explain how to skip TestNG test at runtime:

ExampleTest.java

Java
package testNG;
import org.testng.SkipException;
import org.testng.annotations.Test;

public class ExampleTest {
	
	@Test
    public void skipTest() {
        // Directly skip the test
		System.out.println("Skipping this test without any condition.");
		
        throw new SkipException("Skipping this test without any condition.");
    }
	
	 @Test
	 public void skipDueToMissingData() {
	        String data = null; // Assume data is null due to an external API failure or file read error
	        System.out.println("This test runs if the data is available.");
	        if (data == null) {
	            throw new SkipException("Skipping test due to missing data.");
	        }

	       
	    }
    }

testng.xml

XML
<?xml version = "1.0" encoding = "UTF-8"?>

<suite name="Suite">
    <test name="Test Suite">
        <classes>
           
            <class name="testNG.ExampleTest">

            </class>
        </classes>
    </test>
</suite>

Output

output
Output

Conclusion

TestNG offers us a SkipException class that we can use to skip the tests at runtime. SkipException is a very useful feature because of execution dependency on dynamic conditions such as configuration settings and an environment's state. You can conditionally skip tests without changing test logic by throwing SkipException. This will make your test suite much more flexible and accommodating to various scenarios.


Next Article
Article Tags :

Similar Reads