Skip Test case in TestNG
In TestNg we can skip the test case by using the throw new SkipException(“message”); We may come across the situation like when we do not have sufficient data and we want to skip the test case in that condition then we can use the SkipException .
Java Program :
Here we have two test cases in below . Test case 1 and Test case 2 . Test case 1 is skipped on some certain condition when skip is true by throwing the SkipException.
package com.TestNg; import org.testng.SkipException; import org.testng.annotations.Test; public class Test_Annotation { // Test case is skipped @Test public void TestCase1() { Boolean skip = true; // Skipping Test case on below condition if ( skip== true) { throw new SkipException("Skipping this exception"); } } @Test public void TestCase2() { System.out.println("---Test case 2 executed---"); System.out.println("---Test case 2 ended---"); } }
Output :
---Test case 2 executed--- ---Test case 2 ended--- PASSED: TestCase2 SKIPPED: TestCase1 =============================================== Default suite Total tests run: 2, Failures: 0, Skips: 1 ===============================================
Ignore a TestCase in TestNg
Ignore a Test case in TestNg is some time useful and we may come across the situation where we know that some of the test case will get fail so in such scenario we can ignore the test cases by using the below syntax
@Test(enabled=false) in any test method
Enabled – It is set to true or false and it describes whether method should run or not. By default it is set to true. Which means it it is “false” then test case will not get executed and if it is true the test case will get executed.
Java Program :
In below example Test Case 2 is set to enable = false , hence this test is ignored.
package com.TestNg; import org.testng.annotations.Test; public class Test_Annotation { // Test case 1 @Test(enabled=true) public void TestCase1() { System.out.println("---Test case 1 executed---"); System.out.println("---Test case 1 ended---"); } // Test case 2 @Test(enabled=false) public void TestCase2() { System.out.println("---Test case 2 executed---"); System.out.println("---Test case 2 ended---"); } }
TestNG XML file :
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test_run"> <classes> <class name="com.TestNg.Annotations"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
Output : Only Test case 1 is executed as Test 2 method is set enabled = false hence it will not run.
---Test case 1 executed--- ---Test case 1 ended--- PASSED: TestCase1 =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 ===============================================
Add description in TestNg method
Description : Add the description for the test methods , Here we will learn how to add the description in the Test Case in TestNg.
Java Program
package com.TestNg; import org.testng.annotations.Test; public class Test_Annotation { @Test(description="open Browser") public void TestCase1() { System.out.println("---Test case 1 executed---"); System.out.println("---Test case 1 ended---"); } @Test(description="Close browser") public void TestCase2() { System.out.println("---Test case 2 executed---"); System.out.println("---Test case 2 ended---"); } }
Output : Description of test method is below
---Test case 1 executed--- ---Test case 1 ended--- ---Test case 2 executed--- ---Test case 2 ended--- PASSED: TestCase1 open Browser PASSED: TestCase2 Close browser
This is all about how we can skip test case in TestNg based on the circumstances and ignore test case in TestNg. This blog also covers the selenium interview questions