Saturday, September 28, 2019

Post#117.How to Skip Exception Test in TestNG


How to Skip Exception Test in TestNG
Using TestNG, we have multiple ways to Skip a test based on our requirement. We can Skip complete test without executing it or we can Skip a test when a specific condition is not satisfied.
Case 1: In TestNG, @Test(enabled=false) annotation is used to skip a test case if it is not ready to test. We don't need to import any additional statements.

As in JUnit, TestNG will not show you the other test method as Skipped or Ignored. It will not consider that case method at all when the annotation is mentioned as “@Test(enabled=false)”.
Case 2: And We can Skip a test by using TestNG SkipException if we want to Skip a particular Test.
Syntax: throw new SkipException("message");
Case 3: we can also perform a CONDITIONAL Skip, i.e. we can have a condition to check and then SKIP test if condition is not satisfied. For Example, if there is no data available to perform a test, we can skip the test instead of making that test as failed.
Let us see all the above three cases by taking three simple tests.
Please find the below sample program.

package TestNGPack2;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class SkipExample {
           
public static WebDriver driver = new FirefoxDriver();
String DataAvailable="null";
   
    @BeforeTest
    public void setup() throws Exception {
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://newtours.demoaut.com/");
    }
           
            @Test(enabled=false)
            public void TC001(){
                                    System.out.println("TC001  Executed");
                        }
            @Test
            public void TC002(){
                                    System.out.println("I am in TC002 - Intensional Skip");
                                    String title = driver.getTitle();
                                    if(title.equals("Welcome: Mercury tours"))
                                    {
                //To Skip Test
                                                throw new SkipException("TC002 Is Skipped");
                                    }
                                    System.out.println("TC002 Executed Successfully");
                        }

            @Test
            public void TC003(){
                        System.out.println("Im in Conditional Skip");
                        if(DataAvailable.equals("null"))
                                                {
                                                            throw new SkipException("TC003 is Skipped - Data Not Available");
                                                }
                       
                        System.out.println("TC003 Executed Successfully");
            }
            @AfterTest
    public void tearDown() throws Exception {
    driver.quit();
}

}

Once executed the above program the output will look as below.


As you see the above output,
TC001() method is not executed at all. Because It will not consider that case method at all when the annotation is mentioned as “@Test(enabled=false)”
TC002() method is Executed and passed. Because The Page Title is matched, so it did not go inside if condition. It will skip if the Title will not match with Expected.

TC003() is Skipped. Test TC003 will be skipped only when data is not available. Here I took a simple variable DataAvailable for ease of understanding. The ‘if condition satisfied, hence Skipped the test. You can put condition according your requirement.



No comments:

Post a Comment