Priority and dependsOnMethods Annotation in TestNG

TestNG priorities annotation
TestNG priorities annotation


priority annotation in TestNG

In our previous TestNG tutorial, we learn how to run selenium tests using TestNG. We also learned about various testng annotations and testng.xml. In this tutorial, we will learn how priority and dependsOnMethods annotation in TestNG helps in prioritizing our tests so that they could be executed in some logical order and not in the alphabetical sequence.

Syntax for priority annotation

@Test(priority = 1)
Setup your project for TestNG (Click Here to know how) and create a new class PriorityDependsOnTestNG. Create few test methods in it with an increase in priority.
package testpkg;

import org.testng.Assert;
import org.testng.annotations.Test;

public class PriorityDependsOnTestNG {

    @Test(priority = 1)
    public void firstTest() {
        System.out.println("First Test");        
    }
    
    @Test(priority = 2)
    public void secondTest() {
        System.out.println("Second Test");
    }
    
    @Test(priority = 3)
    public void thirdTest() {
        System.out.println("Third Test");
    }
    
    @Test(priority = 4)
    public void fourthTest() {
        System.out.println("Fourth Test");
    }
    

}

If you run the above test as TestNG Test all the methods/ tests should get executed in the mandated priority. @Test annotation with the lowest priority value would get executed first.


dependsOnMethods annotation in TestNG

Suppose your first method/ test is to test the login functionality, and if this first test gets failed, you cannot test other methods. So there is no point of executing them. Consider the following code with updated dependsOnMethods annotation:
package testpkg;

import org.testng.Assert;
import org.testng.annotations.Test;

public class PriorityDependsOnTestNG {

    @Test(priority = 1)
    public void firstTest() {
        System.out.println("First Test");
        Assert.fail();
        
    }
    
    @Test(priority = 2, dependsOnMethods = {"firstTest"})
    public void secondTest() {
        System.out.println("Second Test");
    }
    
    @Test(priority = 3, dependsOnMethods = {"firstTest"})
    public void thirdTest() {
        System.out.println("Third Test");
    }
    
    @Test(priority = 4, dependsOnMethods = {"firstTest"})
    public void fourthTest() {
        System.out.println("Fourth Test");
    }
    

}

The updated code is highlighted in bold. In the above example, all the tests/ methods from priority 2 till priority 4 are dependent on test having priority 1. We have used Assert.fail() method to simulate the test failure scenario. Now, if this test gets failed, other tests would get skipped. Running the above test would yield in below output:

dependsOnMethods Annotation TestNG

dependsOnMethods annotation does not follow priority but is always executed after the method they are dependent on

This way we can chain all the depending tests and restrict the unnecessary execution of scripts which would eventually fail if their depending scripts gets failed. However, what if we want to execute certain tests even if their depending tests get failed. For instance, a user should be able to perform forgot password if login functionality is not working due to an expired password.


Customize dependsOnMethods annotation in TestNG

TestNG provide another annotation, alwaysRun, which could be assigned true or false to control the test execution. Update the existing code as following so that thirdTest() and fourthTest() tests should always get executed, irrespective of the result of the dependent test.
package testpkg;

import org.testng.Assert;
import org.testng.annotations.Test;

public class PriorityDependsOnTestNG {

    @Test(priority = 1)
    public void firstTest() {
        System.out.println("First Test");
        Assert.fail();
        
    }
    
    @Test(priority = 2, dependsOnMethods = {"firstTest"})
    public void secondTest() {
        System.out.println("Second Test");
    }
    
    @Test(priority = 3, dependsOnMethods = {"firstTest"}, alwaysRun=true)
    public void thirdTest() {
        System.out.println("Third Test");
    }
    
    @Test(priority = 4, dependsOnMethods = {"firstTest"}, alwaysRun=true)
    public void fourthTest() {
        System.out.println("Fourth Test");
    }
    

}

Output of the above test script is:
dependsOnMethods Annotation TestNG alwaysRun

Click Here: To buy java and python ebooks

Click Here: To subscribe and get a free ebook on Selenium POM framework and create your own framework from scratch. 

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

2 thoughts on “Priority and dependsOnMethods Annotation in TestNG

Leave a Reply

Your e-mail address will not be published. Required fields are marked *