TestNG SkipException Example [ 2024 ]
TestNG SkipException Example
You can conditionally skip a test using the SkipException in the TestNG framework. This can be useful when certain preconditions are unmet, and you want to bypass the execution of a particular test.
SkipException Example
Let’s look the below example to illustrate how to use SkipException in a TestNG test:
import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipExceptionExample {
@Test
public void testMethod1() {
System.out.println("testMethod1 is running");
}
@Test
public void testMethod2() {
System.out.println("testMethod2 is running");
boolean shouldSkip = true; // Condition to skip the test
if (shouldSkip) {
throw new SkipException("Skipping this test method as the condition is not met.");
}
System.out.println("This line will not be executed if the test is skipped.");
}
@Test
public void testMethod3() {
System.out.println("testMethod3 is running");
}
}
Explanation
testMethod1: This method will execute normally, printing “testMethod1 is running”.
testMethod2: This method includes a condition (shouldSkip). If the condition is true, a SkipException is thrown, which causes TestNG to skip the rest of the method’s execution. The message “Skipping this test method as the condition is not met.” will be displayed in the test results.
testMethod3: This method will execute normally, printing “testMethod3 is running”.
SkipException Usage
Environment Checks: If a particular environment setup is required and it’s not available, you can skip the test.
Conditional Tests: When tests depend on certain true or false conditions, you can conditionally skip tests.
Feature Toggles: You can skip a test related to a feature that is toggled off.
By using SkipException, you can dynamically control the execution of your tests based on runtime conditions, making your test suite more flexible and robust.
TestNG Tutorials
TestNG Tutorials on this website can be found at:
For more details on the TestNG Framework, visit the official website of TestNG at: