What are TestNG Annotations?

Understand TestNG annotations for effective use

77

If you are using TestNG in the project then you should understand the annotations of it. it helps you to use features of TestNG in right way. For different purposes or conditions, different TestNG annotations can be used.

below is the list of annotations in TestNG

  1. @BeforeSuite: The annotated method will be run before all tests in this suite have run.
  2. @AfterSuite: The annotated method will be run after all tests in this suite have run.
  3. @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
  4. @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
  5. @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
  6. @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
  7. @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
  8. @AfterClass: The annotated method will be run after all the test methods in the current class have been run.
  9. @BeforeMethod: The annotated method will be run before each test method.
  10. @AfterMethod: The annotated method will be run after each test method.
  11. @DataProvider: Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
  12. @Factory: Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].
  13. @Listeners: Defines listeners on a test class. you can implement different listeners in your project
  14. @Parameters: Describes how to pass parameters to a @Test method. to pass the test data, the @parameters annotation can be used.
  15. @Test: Marks a class or a method as part of the test.

 

below is test class for most common use of annotations. we will see all annotations in different posts.

 

package TestNG;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Annotations {


 @BeforeMethod
 public void beforeMethod() {
 System.out.println("The annotated method will be run before each test method.");
 }

@AfterMethod
 public void afterMethod() {
 System.out.println("The annotated method will be run after each test method.");
 }

@BeforeClass
 public void beforeClass() {
 System.out.println("The annotated method will be run before the first test method in the current class is invoked. ");
 }

@AfterClass
 public void afterClass() {
 System.out.println("The annotated method will be run after all the test methods in the current class have been run. ");
 }

@BeforeTest
 public void beforeTest() {
 System.out.println("The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. ");
 }

@AfterTest
 public void afterTest() {
 System.out.println("The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. ");
 }

@BeforeSuite
 public void beforeSuite() {
 System.out.println("The annotated method will be run before all tests in this suite have run.");
 }

@AfterSuite
 public void afterSuite() {
 System.out.println("The annotated method will be run after all tests in this suite have run.");
 }

@Test
 public void test1() {
 System.out.println("This is my First Test Case no 1");
 }

@Test
 public void test2() {
 System.out.println("This is my Second Test Case no 2");
 }


}

 

below is the outcome of executed code:

[RemoteTestNG] detected TestNG version 6.14.3
The annotated method will be run before all tests in this suite have run.
The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. 
The annotated method will be run before the first test method in the current class is invoked. 
The annotated method will be run before each test method.
This is my First Test Case no 1
The annotated method will be run after each test method.
The annotated method will be run before each test method.
This is my Second Test Case no 2
The annotated method will be run after each test method.
The annotated method will be run after all the test methods in the current class have been run. 
The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
PASSED: test1
PASSED: test2

===============================================
 Default test
 Tests run: 2, Failures: 0, Skips: 0
===============================================

The annotated method will be run after all tests in this suite have run.

===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================


 

 

 

Subscribe to Newsletter
Get Latest Posts, Articles and Learning Lessons on Software Testing and Test Automation
You can unsubscribe at any time

Comments are closed.