Exploring TestNG Annotations and Priorities
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:
a_method
(due to alphabetical order withb_method
)b_method
(due to alphabetical order witha_method
)c_method
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.
Subscribe to my newsletter
Read articles from Rinaldo Badigar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rinaldo Badigar
Rinaldo Badigar
Software Engineer with 3+ years of experience in Automation, with a strong foundation in Manual, API, and Performance testing, ensuring high-quality software delivery for Ecommerce and Banking applications.