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

  • Default Priority: If the priority is not specified for a test method, TestNG assigns a priority of 0. This means that test methods without a specified priority will be executed before those with a specified priority, assuming their names come first alphabetically.
  • Same Priority: When multiple test methods have the same priority, TestNG defaults to executing them in alphabetical order based on their method names.
  • Negative Priority: TestNG allows for negative priority values. This can be useful when you need to ensure certain methods always run before others without having to assign a priority to every test method.
  • Dependency Over Priority: If test methods are configured with dependencies (using the dependsOnMethods or dependsOnGroups attributes), TestNG prioritizes respecting these dependencies over the specified priority. This means a test method won’t run until all of its dependencies have run successfully, regardless of the priority values.
  • Use Cases: Priority is particularly useful in scenarios where tests must be executed in a specific order due to dependencies on the state produced by earlier tests. However, it’s generally a good practice to make tests independent of each other when possible.

要查看或添加评论,请登录

mahenderkar sandeep的更多文章

  • Interview questions with Deloitte round-2

    Interview questions with Deloitte round-2

    Date 30-07-2024 Duration : one hour 1. Self Introduction 2.

  • Selenium WebDriver Methods

    Selenium WebDriver Methods

  • Interview questions with Deloitte round-1

    Interview questions with Deloitte round-1

    Date 29-07-2024 Duration : one hour 1. Self Introduction 2.

    1 条评论
  • Difference between Defect, Bug, Error and Failure

    Difference between Defect, Bug, Error and Failure

    What is a Defect? 1.The variation between the actual results and expected results is known as defect.

  • Levels of Testing

    Levels of Testing

    There are mainly four Levels of Testing in software testing : 1.Unit Testing : checks if software components are…

  • TestNG Annotations

    TestNG Annotations

    First, let's summarize the main annotations used in TestNG, which help in defining the execution order of the test…

    1 条评论
  • Parameterization concept in TestNG

    Parameterization concept in TestNG

    In software testing, particularly when using the TestNG framework for Java, parameterization is a powerful concept that…

  • Grouping concept in TestNG

    Grouping concept in TestNG

    In TestNG, the grouping concept allows you to categorize and organize test methods into logical groups based on certain…

  • Description annotation concept in TestNG

    Description annotation concept in TestNG

    In TestNG, the annotation is used to provide descriptive information about test methods or test classes. This…

  • Enabled Annotation concept in TestNG

    Enabled Annotation concept in TestNG

    In TestNG, there is no built-in annotation like in other testing frameworks such as JUnit 5. TestNG primarily relies on…

社区洞察