Understanding TestNG Annotations and Priorities πŸš€

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πŸŽ‰

Types of TestNG Annotations

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

  1. @BeforeSuite: 🌐 Runs before the execution of all other test methods.

  2. @AfterSuite: 🌌 Runs after the execution of all other test methods.

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

  4. @AfterTest: 🏁 Executes after all test methods within a specified folder.

  5. @BeforeClass: 🚦 Runs before the first method invocation of the current class.

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

  7. @BeforeMethod: πŸš€ Executes before each test method.

  8. @AfterMethod: 🧹 Runs after each test method is executed.

  9. @BeforeGroups: 🀝 Runs before the test cases of a specific group execute (executes only once).

  10. @AfterGroups: 🎭 Runs after the test cases of a specific group execute (executes only once).

Hierarchy in TestNG Annotations

Unveiling the hierarchy among TestNG annotations! From top to bottom:

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.

Multiple Test Case Scenario 🌐

When dealing with multiple test cases, TestNG executes them in alphabetical order by default. For example, a test case annotated as @Test public void alpha() {} will run before another test case annotated as @Test public beta() {}.

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.

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:

The anticipated order of execution is as follows:

  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

In 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.

Β