TestNG in Selenium

TestNG Annotations :

Annotations are nothing but a piece of instruction for the compiler that you apply to classes, methods or variables in your Java code. It is a powerful feature and an integral part of the TestNG framework. 

The TestNG annotations extend the same concept and control the execution sequence of the test code and test methods


List of Annotations in TestNG:

TestNG Annotations Description
@Test Attaches a class or a method to become the part of the test.
@BeforeTest Instructs the method to run before any test method related to the classes which are inside the <test> tag as per the testng.xml file
@AfterTest Halts a method from execution till all the test methods finish their execution. These methods belong to the classes defined in the <test> tag of testng.xml file.
@BeforeMethod Allows a method to run before executing any of the @Test annotated methods.
@AfterMethod Allows a method to take off after all of the @Test annotated methods finish their execution.
@Parameters You can use this annotation for passing the parameters to the test methods.
@DataProvider It marks a method as a data source for the test. Every @DataProvider annotated method must always return the value as Object[ ][ ]. You can use it in any of the @Test annotated methods.
@BeforeClass The method annotated with @BeforeClass gets executed once before the first test method of the current class.
@AfterClass The method annotated with @AfterClass gets run once after finishing all the test methods in the current class.
@BeforeGroups It sets up the method to run before the first test method belonging to any of the groups involved in the execution.
@AfterGroups It sets up the method to run after the execution of all the test methods belonging to any of the groups participating in the test.
@BeforeSuite Any such method will get called before any of the suites runs from the test.
@AfterSuite Any such method will stay its execution until all other methods in the current test suite get executed.
@Factory You use it to execute any specific group of test cases with varied values. It returns an array of test class objects as the Object[ ].
@Listeners You can use them with the test classes for the logging function.


Advantages of Annotations :

  • Wecan group the test cases using the appropriate annotation.
  • You can do parallel testing and sequence the tests.


@Test Annotations :

We have to specify the method with @Test to make it an test case or test script in TestNG, you cannot use main method when you are using the TestNG, if you provide main method then only main method is going to get executed.

Create your First Test like below.

import org.testng.annotations.Test;

public class TestExample {
    public  void firstTest() {
        System.out.println("Hello Moto");

Program For All Annotations :

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 TestNGAnnotationExecution {

	   public void beforeMethod() {
	      System.out.println("@Before Method");
	   public void beforeClass() {
	      System.out.println("@Before Class");
	   public void beforeTest() {
	      System.out.println("@Before Test");
	   public void beforeSuite() {
	      System.out.println("@Before Suite");
	   // Test Case 1
	   public void testSCript1() {
	      System.out.println("@est Case 1");

	   // Test Case 2
	   public void testScript() {
	      System.out.println("@Test Case 2");

	   public void afterMethod() {
	      System.out.println("@After Method");

	   public void afterClass() {
	      System.out.println("@After Class");

	   public void afterTest() {
	      System.out.println("@ After Test");

	   public void afterSuite() {
	      System.out.println("@After Suite");


Leave a Reply

Your email address will not be published. Required fields are marked *