Open In App

TestNG @AfterTest Annotation

Last Updated : 08 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. @BeforeSuite
  2. @AfterSuite
  3. @BeforeTest
  4. @AfterTest
  5. @BeforeClass
  6. @AfterClass
  7. @BeforeMethod
  8. @AfterMethod
  9. @BeforeGroups
  10. @AfterGroups

In this article, we will learn about @AfterTest.

What is @AfterTest?

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

Example of @AfterTest

Let’s understand the @AfterTest 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.

Screenshot-(306)

Package Explorer

Step 4: 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");
  }

}


2. 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:

  • 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 5: Now, we create the AnnotationsTest.xml file to configure the After_Test Class and After_Test2 Class.

XML
<?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.geeksforgeeks.test.After_Test" /> 
               <class name="com.geeksforgeeks.test.After_Test2" /> 
               
        </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

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads