TestNG @AfterTest Annotation (original) (raw)

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

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.Test; import org.testng.annotations.AfterTest;

public class After_Test {

@AfterTest public void afterTest() { System.out.println("This method will be executed when all @Test annotated methods complete the execution"); }

@Test public void test1() { System.out.println("Test1 Executed"); }

@Test public void test2() { System.out.println("Test2 Executed"); }

}

`

**Step 2. Create another class After_Test2.Java.

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.Test;

public class After_Test2 { @Test public void test3() { System.out.println("test3 executed"); } @Test public void test4() { System.out.println("test4 executed"); } }

`

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

**afterTest (@AfterTest):

**Test Methods (@Test):

Each test method prints their respective statement.

**After_Test2 Class

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.

XML `

    </classes>
</test>

`

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