VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/how-to-skip-testng-test-at-runtime/

⇱ How to skip TestNG test at runtime? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to skip TestNG test at runtime?

Last Updated : 23 Jul, 2025

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

testng.xml

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.

Comment
Article Tags:

Explore