Example of @AfterGroups
Let’s understand the @AfterGroups annotation through an example.
Step 1: Open the Eclipse IDE.
Step 2: Create a Maven Project.
Step 3: After Creating the Maven Project, the project exploration will look like the below image.
Step 4: Create a TestNG Class that contains @AfterGroups.
After_Groups.Java
package com.w3wiki.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.w3wiki.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):
- Each test method is annotated with @Test, indicating that it is a test case.
- There are four test methods: fun1(), fun2(), fun3() and fun4().
- Each test method prints their respective statement.
- After performing the operation, the result is printed to the console.
Step 5: Now, we create the AnnotationsTest.xml file to configure the After_Groups class.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite">
<test name="test1">
<classes>
<class name="com.w3wiki.test.After_Groups" />
</classes>
</test>
</suite>
Step 6: Run the AnnotationsTest.xml. Right click on the AnnotationsTest.xml file, move the cursor down to Run As and then click on the 1 TestNG Suite.
Output
TestNG Annotations – @AfterGroups
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG.
In TestNG, there are 10 Annotations
In this article, we will learn about @AfterGroups.
Contact Us