Thursday 14 June 2018

What is TestNG Annotations

Hi everyone in this post we will learn basics of TestNG annotations. In our previous tutorial we have seen introduction and setup of TextNG. Now lets learn what is TestNG annotations and how to use TestNG annotations to define proper test suit execution flow.

TestNG Annotation

Lets understand the simple example given below which will help you understand how annotations helps to define the flow.In TestNG annotations @Test is the smallest annotation here. @Method will be executed first, before and after the execution of @Test. The same way @Class will be executed first, before and after the execution of @Method and so on.

Video Tutorial -

 

Do try this out and post your feedback and questions in the comment section below.


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

1 comment: