Site icon TestingDocs.com

TestNG Test Priority

Overview

In this post, we will learn some code examples about TestNG priority and how to set to @Test methods.By default TestNG executes the tests in order of the names and not in the order you write test methods in a class. To demonstrate it we will see code examples.

Code Example 1

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestNGPriorityExample 
{
  @Test 
   public void aMethod(){ 
    System.out.println("TestingDocs >> aMethod"); 
   } 
    
   @Test 
   public void bMethod(){ 
    System.out.println("TestingDocs >> bMethod"); 
   } 
    
   @Test 
   public void cMethod(){ 
    System.out.println("TestingDocs >> cMethod"); 
   }

}

 

PASSED: aMethod
PASSED: bMethod
PASSED: cMethod

===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================

Code Example 2

In this example we will reverse the order of methods written in the class and execute it. You can notice that there is no change in the TestNG default order of execution in both the examples.

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestNGPriorityExample 
{
  @Test 
   public void cMethod(){ 
    System.out.println("TestingDocs >> cMethod"); 
   }
  
   @Test 
   public void bMethod(){ 
    System.out.println("TestingDocs >> bMethod"); 
   } 
  
  @Test 
   public void aMethod(){ 
    System.out.println("TestingDocs >> aMethod"); 
   } 
    
}

 

PASSED: aMethod
PASSED: bMethod
PASSED: cMethod

===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================

 

Now we want the methods to execute in a particular order  lets say : c —-> b ——–> a

TestNG framework provides an attribute to @Test annotation called priority. Priority controls the run scheduling order of the test method. Lower priorities will be scheduled first.

We will achieve the order of execution in the below code example. Notice how we set priority to the test methods.We will set cMethod lower priority so that the methods executes first. bMethod executes next with priority 2 and then aMethod with priority set to 3.

Code Example 3

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestNGPriorityExample 
{
  @Test(priority=3) 
   public void aMethod(){ 
    System.out.println("TestingDocs >> aMethod"); 
   } 
  
   @Test(priority=2) 
   public void bMethod(){ 
    System.out.println("TestingDocs >> bMethod"); 
   } 
  
  @Test(priority=1) 
   public void cMethod(){ 
    System.out.println("TestingDocs >> cMethod"); 
   }
    
}

 


PASSED: cMethod
PASSED: bMethod
PASSED: aMethod

===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================

 

 

Now we will see one exception for the order of execution using the priority of the test methods. In the next example, we set method dependency and force to the reverse order of execution using priorities to test methods.

In the below example method2 depends on method1. So even if method2 is set lower priority to execute first, since it is dependent on method1, method1 we will executed first.

Code Example 4

public class TestNGPriorityExample2
{
  
  @Test(priority=2) 
   public void method1(){ 
    System.out.println("TestingDocs >> In method1"); 
    Assert.assertEquals("PASS", "PASS"); 
   } 
    
     
   @Test(priority=1,dependsOnMethods={"method1"}) 
   public void method2(){ 
    System.out.println("TestingDocs >>  method2"); 
   } 
 

}

 

PASSED: method1
PASSED: method2

===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================

 

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Exit mobile version