Some basic annotation of TestNG

Below are some basic annotation which we use in automation.
 
@BeforeSuite - Execute only one time before all testNg method..
@AfterSuite - Execute only one time after all testNg method.
@BeforeClass - Execute only one time before first test and after @BeforeSuite testNg method.
@AfterClass - Execute only one time after last test and before @AfterSuite testNg method.
@BeforeMethod - Execute before every @Test methods.
@AfterMethod - Execute after every @Test methods.
@Test – TestNG test method.


Below is example for the TestNG annotations:

package com.test;

import org.testng.annotations.*;

public class TestNGBasicAnnotation {

   @BeforeSuite
   public void suite(){
      System.out.println("@Befor Suite - one time executed before any testNG function");
   }
  
   @AfterSuite
   public void afterSuite(){
      System.out.println("@After Suite - one time executed after all testNg function");
   }

   @BeforeClass
   public void beforeClass() {
        // one-time initialization code
      System.out.println("@BeforeClass - one time executed before first mathod");
   }
   @AfterClass
   public void afterClass() {
      // one-time cleanup code
      System.out.println("@AfterClass - one time executed after last method");
   }
   @BeforeMethod
   public void setUp() { 
       System.out.println("@BeforeMethod - executed before all test method");
   }
   @AfterMethod
   public void tearDown() { 
       System.out.println("@AfterMethod - executed after all test methods");
   }
  
   @Test
   public void testFirstMethod() {   
      System.out.println("@Test - executed first test method");
   }
   @Test
   public void testSecondMethod() {   
      System.out.println("@Test - executed second test method");
   }
}

Below are the execution log after executing above TestNG class. 

@Befor Suite - one time executed before any testNG function
@BeforeClass - one time executed before first mathod
@BeforeMethod - executed before all test method
@Test - executed first test method
@AfterMethod - executed after all test methods
@BeforeMethod - executed before all test method
@Test - executed second test method
@AfterMethod - executed after all test methods
@AfterClass - one time executed after last method
@AfterSuite - one time executed after all testNg functions
PASSED: testFirstMethod
PASSED: testSecondMethod

No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution