Priority concept in TestNG
The priority attribute in TestNG plays a crucial role in managing the execution order of test methods. By default, TestNG executes test methods in the alphabetical order of their names. However, in real-world scenarios, this default behavior may not be desirable or sufficient, as some tests might need to be executed before others due to dependencies among them. This is where the priority attribute becomes particularly useful.
How Priority Works
The priority attribute is used within the @Test annotation to specify the order in which test methods should be executed. It takes an integer value, and TestNG executes test methods in ascending order based on their priority values. If no priority is set, TestNG assigns a default priority of 0 to the test method.
Example Usage
package priorities;
import org.testng.annotations.Test;
public class PriorityTestng {
@Test(priority = 1)
public void display()
{
System.out.println(" display method");
}
@Test(priority = 2)
public void show()
{
System.out.println("show method");
}
@Test(priority = 3)
public void print()
{
System.out.println("print method");
}
}
Key Points