Skip / Disable / Ignore Tests in TestNG:
- Sometimes we can face a situation where our test cases can not be prepared and we have to stop those tests from running due to exception issue’s.
Ways to Skip / Disable / Ignore Tests in TestNG:
We can achieve this requirement in three ways:
- Making parameter “enabled” as “false”.
- Using “exclude” parameter in testng.xml.
- “throw new SkipException()” in the if condition to Skip / Ignore Test.
1. Making parameter “enabled” as false:
- Using @Test(enabled = false) this method. We are able to skip / ignore method.
- Using @Test(enabled = false) method we can not run the method it simply skip / ignore method and execute next one.
- If we want to execute / run method then simply add true above the method e.g: @Test(enabled = true) .
Example: Skip / Ignore method using @Test(enabled = false)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import org.testng.Assert; import org.testng.annotations.Test; public class DisableTestDemo { @Test(enabled = true) public void testMethodOne() { System.out.println("Test method one."); } @Test(enabled = false) public void testMethodTwo() { System.out.println("Test method two."); } @Test public void testMethodThree() { System.out.println("Test method three."); } } |
- Right click on the testng.xml and run it as ‘TestNG Suite’ to execute script.
Output:
1 2 3 | Test method one. Test method three. |
2. Using “exclude” parameter in testng.xml:
- TestNg provides an option to include or exclude for Groups, Test Methods, Classes and Packages using include and exclude tags by defining in testng.xml.
- Only include methods will be Execute / Run other methods will be skip / ignore.
- Using <exclude name=”method_name” /> this method. We are able to skip / ignore method.
1 2 3 | // Skip / Ignore Test Methods. <exclude name="method_name" /> |
- Using < include name=”method_name” /> this method. Execute method properly.
1 2 3 | // Execute Test Methods. <include name="method_name" /> |
- We will create a Class with three Test Methods. In that we will include two test methods and try to exclude one test method.
Example: class file with three test methods.
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 package_Name; import org.testng.annotations.Test; public class Class_Name { @Test public void method_1() { System.out.println("Im in method_1 test case"); } @Test public void method_2() { System.out.println("Im in method_2 test case"); } @Test public void method_3() { System.out.println("Im in method_3 test case"); } } |
Example: Use Exclude / Include in TestNG
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Sample Test Suite" verbose="1" > <test name="Method Test Cases" > <classes> <class name=" package_Name.Class_Name"> <methods> <include name="method_1" /> <exclude name="method_2" /> <include name="method_3" /> </methods> </class> </classes> </test> </suite> |
Output:
1 2 3 4 5 6 7 | Im in method_1 test case Im in method_3 test case PASSED: method_1 PASSED: method_3 |
3. skipping a test method is by using throw new SkipException() exception:
- Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.
Example: Skip / Ignore Test in TestNG using SkipException()
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 Skip_Ignore_Test { @Test public void skip_Ignore_Test (){ String a ="Skip / Ignore Test"; if(a.equals("Skip / Ignore Test")){ throw new SkipException("Skipping / Ignoring - Script not Ready for Execution "); }else{ System.out.println("In else condition"); } System.out.println("Out of the if else condition"); } @Test public void out_Of_Skip_Test(){ System.out.println("No need to skip / ignore test"); } } |
Output:
1 2 3 4 | [RemoteTestNG] detected TestNG version 6.13.1 No need to skip / ignore test PASSED: out_Of_Skip_Test SKIPPED: skip_Ignore_Test |
How to skip or ignore execution of tests in TestNG and TestNG Ignore or Disable Tests and Skip Exception Test in TestNG