Showing posts with label TestNG Basics. Show all posts
Showing posts with label TestNG Basics. Show all posts

Priority in TestNG

 



What is priority in TestNG?


The TestNG Priority is a mechanism used to force a specific execution order among test methods that belong to the same TestNG class. By default, TestNG does not guarantee any particular order for running test methods within a class; they might run in alphabetical order or another unpredictable sequence. Priority allows the developer to override this default behavior.


In TestNG, the priority attribute is used to define the order of execution of test methods in a class.


By default, TestNG executes test methods in alphabetical order of method names (not the order they appear in the code). If you want to control the execution order explicitly, you can assign a priority to each @Test method.














Key Points about priority:

  • The lower the priority number, the earlier the method will run.

  • priority = 0 will run before priority = 1, and so on.

  • If no priority is specified, TestNG assigns it a default priority of 0.

  • You can assign negative values as well.

  • If two or more tests have the same priority, they run in alphabetical order.



Example Java Code for Priority in TestNG

Below is the test class having multiple test methods having attribute priority in @Test annotation.
Output is shown below of the execution of test methods depends on priority values.

import org.testng.annotations.Test;

public class PriorityExample {

    @Test(priority = 1)
    public void testC() {
        System.out.println("Test C - Priority 1");
    }

    @Test(priority = 0)
    public void testA() {
        System.out.println("Test A - Priority 0");
    }

    @Test(priority = 2)
    public void testB() {
        System.out.println("Test B - Priority 2");
    }

    @Test  // No priority mentioned, default is 0
    public void testD() {
        System.out.println("Test D - Default Priority (0)");
    }
}



Output:

Test A - Priority 0
Test D - Default Priority (0)
Test C - Priority 1
Test B - Priority 2



Important points:
  • testA() and testD() both have priority 0. Since testA() comes alphabetically before testD(), it runs first.
  • Priorities help in defining explicit flow of tests, especially in dependent or staged testing scenarios.
  • In TestNG, when multiple test methods have the same priority, TestNG does not guarantee any specific execution order among them. It means that the methods with the same priority may be executed in any order, and this order can change across different runs.
  • Lower priority value = executed earlier
  • Same priority value = no guaranteed order among those methods



Example Code: Same Priority Test Methods

Below in test class, first three test methods have same priority that 1, last method has priority as 0.
Below is the output shown the execution flow of all test methods. Please have a look.

import org.testng.annotations.Test;

public class SamePriorityExample {

    @Test(priority = 1)
    public void testA() {
        System.out.println("Test A - Priority 1");
    }

    @Test(priority = 1)
    public void testB() {
        System.out.println("Test B - Priority 1");
    }

    @Test(priority = 1)
    public void testC() {
        System.out.println("Test C - Priority 1");
    }

    @Test(priority = 0)
    public void testD() {
        System.out.println("Test D - Priority 0");
    }
}



Output:

Test D - Priority 0
Test A - Priority 1
Test B - Priority 1
Test C - Priority 1



Another Run Might Give:

Test D - Priority 0
Test C - Priority 1
Test A - Priority 1
Test B - Priority 1


If multiple test methods share the same priorityTestNG may run them in any order. So, do not rely on order of execution if you assign the same priority to multiple test methods.


Suggested Posts:

1. Introduction to TestNG
2. Annotations in TestNG
3. TestNG Asserts
4. First Script in TestNG
5. Parameterization in TestNG

TestNG Annotations

 



Testng is a testing framework inspired by JUnit and NUnit, designed for test configuration flexibility and powerful reporting in Java. Annotations in TestNG control how and when test methods are executed.

TestNG annotations are special markers that you use to logically structure and control the execution flow of your tests. They tell the framework what a particular method is for and when it should be run relative to other tests, groups, classes, or the entire test suite.











Common Testng Annotations

AnnotationDescription
@BeforeSuiteRuns once before all tests in the suite
@AfterSuiteRuns once after all tests in the suite
@BeforeTestRuns before <test> tag in TestNG XML file
@AfterTestRuns after <test> tag in TestNG XML file
@BeforeClassRuns before the first method in the current class
@AfterClassRuns after all methods in the current class
@BeforeMethodRuns before each @Test method
@AfterMethodRuns after each @Test method
@TestMarks a method as a test case
@DataProviderProvides data for parameterized tests
@ParametersInjects parameters from XML into test methods
@BeforeGroupsRuns before the first method of the specified group
@AfterGroupsRuns after the last method of the specified group


Example java code of Testng Annotations:

Below is the TestNG test class contains all annotations as mentioned above. Statement will be printed on console on execution of particular annotation. @BeforeMethod and @AfterMethod will execute with each test associated with @Test. 

Below in table order of annotations is also mentioned, that in which order annotations will be executed.
import org.testng.annotations.*;

public class TestNGAnnotationsExample {

    @BeforeSuite
    public void beforeSuite() {
        System.out.println("→ @BeforeSuite: Runs before the entire test suite");
    }

    

    @BeforeTest
    public void beforeTest() {
        System.out.println("→ @BeforeTest: Runs before <test> section in XML");
    }



    @BeforeClass
    public void beforeClass() {
        System.out.println("→ @BeforeClass: Runs before the first method of the class");
    }

   
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("→ @BeforeMethod: Runs before each @Test method");
    }

   

    @Test
    public void testCase1() {
        System.out.println("✔ Running Test Case 1");
    }

    @Test
    public void testCase2() {
        System.out.println("✔ Running Test Case 2");
    }


    @AfterMethod
    public void afterMethod() {
        System.out.println("→ @AfterMethod: Runs after each @Test method");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("→ @AfterClass: Runs after all test methods of the class");
    }

    @AfterTest
    public void afterTest() {
        System.out.println("→ @AfterTest: Runs after <test> section in XML");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("→ @AfterSuite: Runs after the entire test suite");
    }
}


Order of Execution of TestNG Annotations

OrderAnnotationExecuted When
1@BeforeSuiteOnce before the entire test suite starts
2@BeforeTestBefore <test> tag in the XML file
3@BeforeGroupsBefore any test method belonging to a specified group
4@BeforeClassBefore the first test method in the current class
5@BeforeMethodBefore each test method (@Test)
6@TestActual test case
7@AfterMethodAfter each test method (@Test)
8@AfterClassAfter all test methods in the current class
9@AfterGroupsAfter all test methods belonging to a specified group
10@AfterTestAfter <test> tag in the XML file
11@AfterSuiteOnce after the entire test suite finishes


Execution Flow of Testng annotations

Below is the flow of TestNG annotations, we can see @BeforeMethod and
@AfterMethod executed with each Test associated with @Test annotation. Since we
have, two test cases which are annotated with @Test so @BeforeMethod and
@AfterMethod executed two times.

@BeforeSuite
@BeforeTest
@BeforeGroups
@BeforeClass

@BeforeMethod
@Test
@AfterMethod

@BeforeMethod
@Test
@AfterMethod

@AfterClass
@AfterGroups
@AfterTest
@AfterSuite



Suggested Posts:

1. First TestNG Script
2. TestNG Dependent Tests
3. TestNG Reports
4. Parameterization in TestNG
5. TestNG Reporter Log