Saturday, September 28, 2019

Post#118.TestNG Test Case Priority


Prioritizing Tests in TestNg
Prioritizing tests in testng will discuss about the order of execution tests in test suite. We will write test methods our own way using @Test annotation. After writing the test cases we will execute the test cases either normally or from the testng.xml file. After execution of the tests if we observe the order of execution then we can find that tests executed by taking the alphabetical order. And all the tests will have the equal priority as we did not set any priority to the tests.

In order to set the priority to the tests then we can use one of the @Test attributes called “Priority”. By default all the tests will have the same priority called Zero(i.e. If you not set any priority then it will take the priority as Zero).
Let me explain this with below example.
First of all create one Class as PriorityExample.Java as below.

PriorityExample.Java
package PrioritySet;

import org.testng.annotations.Test;

public class PriorityExample {
            @Test
              public void Register() {
                          System.out.println("===Register TestCase Executed===");
              }
            @Test
              public void Login() {
                          System.out.println("===Login TestCase Executed===");
              }
             @Test
              public void LogOut() {
                          System.out.println("===LogOut TestCase Executed===");
              }
}
If you will run above test in eclipse then console output will looks like bellow.
===LogOut TestCase Executed===
===Login TestCase Executed===
===Register TestCase Executed===
If we observe the above output of all the 3 methods they executed in the alphabetical order.
As per console output, LogOut() @Test method Is executed first, then Login() @Test method and at last Register() @Test method. So this is strange and not as per my requirement. It should execute Register() @Test 1st, Login()@Test 2nd and LogOut() @Test 3rd.
Above issue can be resolve by setting Priority of test.
You can set priority with @Test annotation to correct test execution sequence as bellow.

PriorityExample.Java
package PrioritySet;

import org.testng.annotations.Test;

public class PriorityExample {
            @Test(priority=1)
              public void Register() {
                          System.out.println("===Register TestCase Executed===");
              }
            @Test(priority=2)
              public void Login() {
                          System.out.println("===Login TestCase Executed===");
              }
             @Test(priority=3)
              public void LogOut() {
                          System.out.println("===LogOut TestCase Executed===");
              }
}
If you will run above test in eclipse then console output will looks like bellow.
===Register TestCase Executed===
===Login TestCase Executed===
===LogOut TestCase Executed===
If we observe the above output of all the 3 methods they executed as per the priorities we set.
The test without priority executed first as the default priority is equal to ZERO and it takes the high priority.
This way you can set test execution priority for each and every @Test method of your class.


No comments:

Post a Comment