Thursday, August 14, 2014




In this tutorial, I will introduce you with TestNG annotations list. TestNG acts as a controller. TestNG controls the flow of execution of code with the help of annotations.

TestNG Annotations List


Annotations Description
@BeforeSuite The annotated method will run before all test methods run in this suite.
@AfterSuite The annotated method will run after all test methods run in this suite.
@BeforeTest The annotated method will run before any test method belonging to the classes.
@AfterTest The annotated method will run after all the test methods belonging to the classes.
@BeforeGroups The annotated method will run before the first test method that belongs to any of these groups is invoked.
@AfterGroups The annotated method will run after the last test method that belongs to any of these groups is invoked.
@BeforeClass The annotated method will run before the first test method run in the current class.
@AfterClass The annotated method will run before all the first test methods run in the current class.
@BeforeMethod The annotated method will run before each test method.
@AfterMethod The annotated method will run after each test method.
@DataProvider The annotated method provide data to the @Test methods. The annotated method returns an Object[][].
@Test The annotated method contains test cases.


To understand more about TestNG Annotations and attributes (i.e. @Test, @DataProvider,... etc) with examples then look at Data-Driven Framework tutorial.





Let's understand the TestNG Annotations list with an example


package testng;

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

public class TestNGAnnotations {

 @BeforeSuite
 public void instantiate() {
  System.out.println("Instantiate Object");
 }

 @BeforeTest
 public void dataBaseConnection() {
  System.out.println("Database Connected");
 }

 @BeforeMethod
 public void BeforeMethod() {
  System.out.println("Run before each Test Case.");
 }

 @Test
 public void testCase1() {
  System.out.println("First Test Case Result..... ");
 }
 
 @Test
 public void testCase2() {
  System.out.println("Second Test Case Result.... ");
 }

 @AfterMethod
 public void AfterMethod() {
  System.out.println("Run after each Test Case");
 }

 @AfterTest
 public void dataBaseDisconnection() {
  System.out.println("Database Disconnected");
 }

 @AfterSuite
 public void destory() {
  System.out.println("Destory Object");
 }
}

 

How To Create testng.xml


testng.xml: Right click on Project --> TestNG --> Convert to TestNG.



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="testng.TestNGAnnotations"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->



How To Run Or Execute TestNG


Right click on testng.xml --> Run As --> TestNG Suite OR Right click on java file --> Run As --> TestNG Test


Output:

TestNG annotations list


Thanks for reading TestNG Annotations List.