VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/testng-aftertest-annotation/

⇱ TestNG @AfterTest Annotation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TestNG @AfterTest Annotation

Last Updated : 6 Aug, 2025

@AfterTest is one of the TestNG Annotations. As the name defines, @AfterTest is executed after the execution of all the @test annotated methods inside a TestNG Suite. This annotation allows developers to specify various actions to be taken after the execution of all the @test annotated methods inside a TestNG Suite.

Let’s understand the @AfterTest annotation through an example.

Step 1: In the Maven project, create a TestNG Class that contains @AfterTest.

1. After_Test.Java


Step 2. Create another class After_Test2.Java.

Now, let’s explain what this code does:

  • Package Declaration: Both the Class is on the com.geeksforgeeks.test package.
  • Imports: Both Class imports annotations and classes from the TestNG framework (org.testng.annotations.BeforeMethod and org.testng.annotations.Test).
  • After_Test Class: This is the main test class. It contains test methods and afterTest method.

afterTest (@AfterTest):

  • This method is annotated with @AfterTest, indicating that it should be executed after test execution this suite contains many classes.
  • It prints this "This method will be executed when all @Test annotated methods complete the execution" statement.

Test Methods (@Test):

  • Each test method is annotated with @Test, indicating that it is a test case.
  • There are two test methods: test1() and test2().

Each test method prints their respective statement.

After_Test2 Class

  • This class doesn't contain @AfterTest annotated method.
  • It contains two methods: test3() and test4().
  • Each test method prints their respective statement.

After performing the operation, the result is printed to the console.

Step 3: Now, we create the AnnotationsTest.xml file to configure the After_Test Class and After_Test2 Class.

Step 4: 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 of AfterTest Annotation
output of AfterTest Annotation

As we can see in above output @AfterTest annotated method will be executed when all @Test annotated methods will complete their execution of both class.

Comment
Article Tags:
Article Tags:

Explore