Saturday, 23 June 2018

TestNG Priority and Sequencing

Hi everyone, in this post we will see how to priorities Test cases in TestNG and how to sequence it as per our requirement. If you create a test class and add multiple test cases and run it then each time test case execution sequence is randomly selected .
With TestNG annotations @Test and parameter "priority" we can define sequence and priority for executing test cases. Let us consider the below example and if you run the test class given below multiple times you can see that each time execution sequence/flow is different hence to define a fixed execution flow we user TestNG annotation priority.

Video Tutorial -



package test;
import org.testng.annotations.Test;
 
public class TesNGSequencing {
 
 
 
  @Test
 
  public void One() {
 
      System.out.println("Test Case number One");
 
  }
 
  @Test
 
  public void Two() {
 
   System.out.println("Test Case number Two");
 
  }
 
  @Test
 
  public void Three() {
 
   System.out.println("Test Case number Three");
 
  }
 
  @Test
 
  public void Four() {
 
   System.out.println("Test Case number Four");
 
  }
 
}

Given below is the example for using priority annotations. When you run the below test class then each time execution flow is constant and starts with test case having 0 priority.


package test;
import org.testng.annotations.Test;
 
public class TesNGSequencing {
 
 
 
  @Test(priority=0)
 
  public void One() {
 
      System.out.println("Test Case number One");
 
  }
 
  @Test(priority=1)
 
  public void Two() {
 
   System.out.println("Test Case number Two");
 
  }
 
  @Test(priority=2)
 
  public void Three() {
 
   System.out.println("Test Case number Three");
 
  }
 
  @Test(priority=3)
 
  public void Four() {
 
   System.out.println("Test Case number Four");
 
  }
 
}

Skipping a Test -


If you come across a situation where there is requirement to skip some tests then in this case you can do it using @Test annotations and enabled parameter.




package test;
import org.testng.annotations.Test;
 
public class TesNGSequencing {
 
 
 
  @Test(priority=0, enabled=false)
 
  public void One() {
 
      System.out.println("Test Case number One");
 
  }
 
  @Test(priority=1)
 
  public void Two() {
 
   System.out.println("Test Case number Two");
 
  }
 
  @Test(priority=2)
 
  public void Three() {
 
   System.out.println("Test Case number Three");
 
  }
 
  @Test(priority=3)
 
  public void Four() {
 
   System.out.println("Test Case number Four");
 
  }
 
}

2 comments:

  1. The information you share is very useful. It is closely related to my work and has helped me grow. Thank you!
    wuxiaworld

    ReplyDelete
  2. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete