TestNG Annotations @AfterGroups (original) (raw)

Last Updated : 6 Aug, 2025

@AfterGroups is one of the TestNG Annotations. As the name defines, @AfterGroups should be executed after all the test methods belonging to a specified group have been run. This annotation allows developers to specify various actions to be taken after all the test methods belonging to a specified group have been run.

Let's understand the @AfterGroups annotation through an example.

**Step 1: In the Maven Project, create a TestNG Class that contains @AfterGroups.

**After_Groups.Java

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.AfterGroups; import org.testng.annotations.Test;

public class After_Groups {

@AfterGroups("Frontend Testing")  
public void after_group()  
{  
System.out.println("Above are the frontend testing type");  
}  
@Test(groups= {"Frontend Testing"})  
 public void fun1() {
     System.out.println("Unit Testing");
 }
 @Test(groups= {"Frontend Testing"})
 public void fun2() {
     System.out.println("Integration Testing:");
 }
 @Test(groups= {"Frontend Testing"})
 public void fun3() {
     System.out.println("Regression Testing");
 }
 @Test(groups= {"Backend Testing"})
 public void fun4() {
     System.out.println("Structural testing");
 }

}

`

**Now, let's explain what this code does:

**Package Declaration: After_Groups Class is on the com.geeksforgeeks.test package.

**Imports: After_Groups Class imports annotations and classes from the TestNG framework (org.testng.annotations.AfterGroups and org.testng.annotations.Test).

**After_Group Class: This is the main test class, It contains test methods and after groups method.

**afterGroups (@AfterGroups): This method is annotated with @AfterGroups, indicating that it should be executed after groups execution. It prints this "Above are the frontend testing type" statement.

**Test Methods (@Test):

**Step 2: Now, we create the AnnotationsTest.xml file to configure the After_Groups class.

XML `

    </classes>
</test>

`

**Step 3: Run the AnnotationsTest.xml. Right click on the AnnotationsTest.xml file, move the cursor down to Run As and then click on the TestNG Suite.

Output of AfterGroups

Output of AfterGroups