What is TestNG annotations with example

TestNG annotation is the backbone of the automation. Let’s see What are annotations in TestNG and how to use TestNG annotations with example.

What is TestNG annotation?

TestNG annotation also is used to control the flow of the execution of the automation suite. It starts with @ sign. Below is the list of TestNG annotations.

List of TestNG annotation

@BeforeSuite

When a method mark with @BeforeSuite annotation will be executed once only before the suite.

 @BeforeSuite

 public void BeforeSuite() {

    System.out.println("Before Suite");

 }

@AfterSuite

When a method mark with @AfterSuite annotation will be executed once only after the suite.

@AfterSuite

 public void AfterSuite() {

    System.out.println("Before Suite");

 }

@BeforeTest

When a method mark with @BeforeTest annotation will be executed once only before the first @Test

in the testng.xml file.

@BeforeTest

public void BeforeTest() {

    System.out.println("Before Test");

}

@AfterTest

When a method mark with @AfterTest annotation will be executed once only after the All @Test.

It will execute after all @Test in the testng.xml file.

@AfterTest

public void AfterTest() {

    System.out.println("Before Test");

}

@BeforeClass

A method with the annotation @BeforeClass will be invoked before the current class.

 @BeforeClass

public void BeforeClass() {

    System.out.println("Before Class");

}

@AfterClass

A method with the annotation @AfterClass will be invoked after the current class.

@AfterClass

public void AfterClass() {

    System.out.println("Before Class");

}

@BeforeMethod

A method with the @BeforeMethod annotation will be invoked before every test method.

@BeforeMethod

public void BeforeMethod() {

    System.out.println("Before Method");

}

@AfterMethod

A method with the @AfterMethod annotation will be invoked after every test method.

@AfterMethod

public void AfterMethod() {

    System.out.println("Before Method");

}

@BeforeGroup

A method with the @BeforeGroup annotation will be invoked before the test Group.

@BeforeGroups

public void BeforeGroup() {

    System.out.println("Before Method");

}

@AfterGroup

A method with the @AfterGroup annotation will be invoked after the test Group.

@AfterGroups

public void AfterGroup() {

    System.out.println("Before Method");

}

@Test

Mark a class or a method as part of the test case.

@Test

  public void M1() {

      System.out.println("This is my first TestNG program");

  }

@Parameters

@Parameters in TestNG is a data-driven approach to manage the data at run time. Data is defined in the testng.xml side the <Parameter> tag as a variable. That variable will be a global variable. So we can easily use it in the test case method by using “@Parameters({ “parameter-name” })”. Let’s have an example to use @Parameters in TestNG.

@DataProvider

 @DataProvide annotation is another important feature of testNG which is used to perform Data-driven Automation.

@Factory

 

@Listeners