How To Skip TestNG Test:
Let’s see how to skip TestNG test deliberately. Sometimes we may face a situation where our test cases might not be ready and we need to skip those tests from running. One way of skipping a test method is by using throw new SkipException() exception.
Scenario: Skip TestNG Test, If a condition met else continue execution.
Let see a sample WebDriver test case example where I have placed SkipException() inside if condition to Intentionally skip that test.
Note: Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.
Scenario 1:
Let me show you how the below program works by commenting the “throw new SkipException()” in the if condition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package softwareTestingMaterial; import org.testng.annotations.Test; import org.testng.SkipException; public class SkipTestCase { @Test public void aSkipTest(){ String condition ="Skip Test"; if(condition.equals("Skip Test")){ // throw new SkipException("Skipping - This is not ready for testing "); }else{ System.out.println("I am in else condition"); } System.out.println("I am out of the if else condition"); } @Test public void nonSkipTest(){ System.out.println("No need to skip this test"); } } |
Output Console:
1 2 3 4 5 6 7 8 9 |
[TestNG] Running: I am out of the if else condition No need to skip this test =============================================== Default suite Total tests run: 2, Failures: 0, Skips: 0 =============================================== |
Scenario 1:
I will uncomment the “throw new SkipException()” in the if condition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package softwareTestingMaterial; import org.testng.annotations.Test; import org.testng.SkipException; public class SkipTestCase { @Test public void aSkipTest(){ String a ="Skip Test"; if(a.equals("Skip Test")){ throw new SkipException("Skipping - This is not ready for testing "); }else{ System.out.println("I am in else condition"); } System.out.println("I am out of the if else condition"); } @Test public void nonSkipTest(){ System.out.println("No need to skip this test"); } } |
Output Console:
1 2 3 4 5 6 7 8 9 10 |
[TestNG] Running: No need to skip this test PASSED: nonSkipTest SKIPPED: aSkipTest =============================================== Default suite Total tests run: 2, Failures: 0, Skips: 1 =============================================== |
Skip exception thrown and the remaining part of the first test method “aSkipTest” not executed and control reached to second test method “nonSkipTest” and printed the value as “No need to skip this test”
You could find the complete TestNG tutorial here.
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
3 thoughts on “Skip TestNG Test | TestNG Tutorial”
Hello Raj,
I guess it shows no of Skips for SkipException in Results of running.
Skipped: 1
Hi Deepa, thanks for letting me know. You are correct. I have updated that. If you are interested to write on our blog, just follow our guest post link. Thanks.
Thanks Raj.