TestNG Annotations

 HOME

In the previous tutorial, we have discussed How to Download and Install TestNG in Eclipse. In the this tutorial, we are going to discuss about the various annotations available in TestNG. Below is the list of annotations:-

  • @Test – This marks a class or a method as a part of the test.
  • @BeforeSuite – The @BeforeSuite method in TestNG runs before the execution of all other test methods. It will run only once before all tests in the suite have to run
  • @AfterSuite – The @AfterSuite method in TestNG runs after the execution of all other test methods. It will run only once after all tests in the suite have run
  • @BeforeTest – The @BeforeTest method in TestNG runs before the execution of first @Test method. It runs only once per class.
  • @AfterTest – The @AfterTest method in TestNG executes after the execution of all the test methods that are inside that folder.
  • @BeforeClass – The @BeforeClass method in TestNG will run before the first method invokes of the current class.
  • @AfterClass – The @AfterClass method in TestNG will execute after all the test methods of the current class execute.
  • @BeforeMethod – The @BeforeMethod method in TestNG will execute before each test method. 
  • @AfterMethod – The @AfterMethod method in TestNG will run after each test method is executed.
  • @BeforeGroups – The @BeforeGroups method in TestNG run before the test cases of that group execute. It executes just once.
  • @AfterGroups – The @AfterGroups method in TestNG run after the test cases of that group execute. It executes only once. 
  • @Parameters –This annotation is used to pass parameters to test methods.
  • @DataProvider – If we use @DataProvider annotation for any method that means you are using that method as a data supplier. The configuration of @DataProvider annotated method must be like it always return Object[][] which we can use in @Test annotated 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.
  • @Factory – Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].
  • @Listeners – This annotation is used with test class. It helps in writing logs and results.

Lets see a program to understand the sequence in which these annotations run in the program

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 TestNGAnnotationDemo {
    
    // Test Case 1
     @Test
     public void testCase1() {
           System.out.println("This is Test Case 1");
     }
 
     // Test Case 2
     @Test
     public void testCase2() {
           System.out.println("This is Test Case 2");
     }
 
     @BeforeMethod
     public void beforeMethod() {
           System.out.println("This will execute Before Method");
     }
 
     @AfterMethod
     public void afterMethod() {
           System.out.println("This will execute After Method");
     }
 
     @BeforeClass
     public void beforeClass() {
           System.out.println("This will execute Before Class");
     }
 
     @AfterClass
     public void afterClass() {
           System.out.println("This will execute After Class");
     }
 
     @BeforeTest
     public void beforeTest() {
           System.out.println("This will execute Before Test");
     }
 
     @AfterTest
     public void afterTest() {
           System.out.println("This will execute After Test");
     }
 
     @BeforeSuite
     public void beforeSuite() {
           System.out.println("This will execute Before Suite");
     }
 
     @AfterSuite
     public void afterSuite() {
           System.out.println("This will execute After Suite");
     } 
}

Output
 
This will execute Before Suite
This will execute Before Test
This will execute Before Class
This will execute Before Method
This is Test Case 1
This will execute After Method
This will execute Before Method
This is Test Case 2
This will execute After Method
This will execute After Class
This will execute After Test
PASSED: testCase1
PASSED: testCase2

Execution process is as follows:

  1. First of all, beforeSuite() method is executed only once.

2. Lastly, the afterSuite() method executes only once.

3. @BeforeMethod and @AfterMethod are executed twice, one for Test Case 1 and again for Test Case 2.

4. Methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once.

5. This is all about TestNG Annotations.

6. To execute this program, we need to Right click and select Run as – TestNG

 The test execution result will look like something shown below

TestNG generates various type of reports under test-output folder.

Open ‘emailable-report.html‘, as this is a html report open it with browser. It will look like something below.

TestNG also produce “index.html” report and it resides under test-output folder

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s