Exploring TestNG Annotations and Priorities

ยท

3 min read

Introduction

TestNG, a robust testing framework for Java, empowers testers with a versatile set of annotations to streamline test case execution. Let's dive into the world of TestNG annotations, explore their hierarchy ๐ŸŽ‰, and understand how priorities dictate the order of execution.

Types of TestNG Annotations

In the realm of TestNG, ten annotations rule the kingdom:

  • @BeforeSuite: ๐ŸŒ Runs before the execution of all other test methods.

  • @AfterSuite: ๐ŸŒŒ Runs after the execution of all other test methods.

  • @BeforeTest: ๐Ÿš€ Executes before all test methods within a specified folder.

  • @AfterTest: ๐Ÿ Executes after all test methods within a specified folder.

  • @BeforeClass: ๐Ÿšฆ Runs before the first method invocation of the current class.

  • @AfterClass: ๐Ÿ›‘ Executes after all test methods of the current class.

  • @BeforeMethod: ๐Ÿš€ Executes before each test method.

  • @AfterMethod: ๐Ÿงน Runs after each test method is executed.

  • @BeforeGroups: ๐Ÿค Runs before the test cases of a specific group execute (executes only once).

  • @AfterGroups: ๐ŸŽญ Runs after the test cases of a specific group execute (executes only once).

Hierarchy in TestNG Annotations

To visualize, here's a snippet from our intergalactic code:

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class TestNG {
 @Test
 public void testCase1() {
   System.out.println("This is the A Normal Test Case"); 
 }

 @BeforeMethod
 public void beforeMethod() {
   System.out.println("This will execute before every Method");
 }

 @AfterMethod
 public void afterMethod() {
   System.out.println("This will execute after every Method");
 }

 @BeforeClass
 public void beforeClass() {
   System.out.println("This will execute before the Class");
 }

 @AfterClass
 public void afterClass() {
   System.out.println("This will execute after the Class");
 }

 @BeforeTest
 public void beforeTest() {
   System.out.println("This will execute before the Test");
 }

 @AfterTest
 public void afterTest() {
   System.out.println("This will execute after the Test");
 }

 @BeforeSuite
 public void beforeSuite() {
   System.out.println("This will execute before the Test Suite");
 }

 @AfterSuite
 public void afterSuite() {
   System.out.println("This will execute after the Test Suite");
 }
}

The output of the above code follows the hierarchy, starting with @BeforeSuite and ending with @AfterSuite.

Test Priority in TestNG ๐ŸŽฏ

While TestNG annotations determine the order of test execution, priorities add an extra layer of control. Priorities, specified using the priority attribute, dictate the order in which annotated methods are executed. The larger the priority number, the lower the priority.

If two methods share the same priority, TestNG resorts to alphabetical order for execution. This ensures a consistent and predictable sequence.

TestNG Methods With Same Priorities ๐Ÿ”„

When two methods have the same priority, TestNG resorts to alphabetical order for execution. For example:

@Test(priority=1)
public void b_method(){
    System.out.println("B Method");
}

@Test(priority=1)
public void a_method(){
    System.out.println("A method");
}

This would result in alphabetical execution order, running b_method before a_method.

TestNG Test Case With and Without Priority ๐Ÿ”ข

Combining test cases with and without priority options showcases how TestNG handles execution. For instance:

import org.testng.annotations.Test;

public class TestNG {
  @Test (priority = 1)
  public void b_method() {
    System.out.println("This is B method");
  }

  @Test (priority = 1)
  public void a_method() {
    System.out.println("This is A method");
  }

  @Test
  public void d_method() {
    System.out.println("This is D Method");
  }

  @Test
  public void c_method() {
    System.out.println("This is C Method");
  }
}

In TestNG, the order of test method execution depends on the specified priorities and, in the absence of priorities, on the default behaviour (usually in alphabetical order). Let's analyze the order in which the test methods in your provided code will run:

So, the expected order of execution will be:

  1. a_method (due to alphabetical order with b_method)

  2. b_method (due to alphabetical order with a_method)

  3. c_method

  4. d_method

Conclusion

Mastering TestNG annotations and priorities provides testers with precise control over the test execution flow, ensuring a systematic and efficient testing process. Whether organizing test suites or prioritizing critical tests, TestNG annotations offer flexibility and customization for diverse testing needs.

ย