TestNG Annotations: Introduction, Importance and their Uses

Category: Automation Framework, TestNG Tutorials 36 0

In the previous tutorial on TestNG, we discussed the basics of TestNG Framework and its installation steps along with the significance of having TestNG in Automation testing. Today our main agenda is all about TestNG Annotations, because when we are dealing with TestNG Framework then we must deal with TestNG Annotations.

We already know that TestNG is the ‘Next Generation’ test framework, which is inspired by Junit. TestNG Annotation plays a crucial role in designing the test cases. They are effectively used in developing data driven framework. Before we jump to the today’s agenda, let’s understand general Java annotations, after all, TestNG annotations basically uses the concept of Java annotations.

What are Java Annotations?

Java annotations are the tag which starts with ‘@’. Annotations are used to present some metadata which basically carries additional information or instruction which is ultimately used by compiler and JVM.

Annotations are used everywhere like, Methods, Classes, or Interface.

Some of the Java annotations are:

  • @Override
  • @SuppressWarnings
  • @Deprecated
  • @Retention
  • @Target
  • @Inherited
  • @Documented

These are generic Java annotations, not TestNG Annotations. Java allows us to create own custom annotations by following some set of guidelines.

List of TestNG Annotations

You got the basic concept of Annotations. Let’s have a look at TestNG Annotations which we mainly use in designing our test cases in Automation framework. You must keep these annotations in your mind so that you could use it whenever and wherever required while writing your automation scripts.

@BeforeGroups

This annotation will be executed before invocation of the first test method of the same group.

@BeforeSuite

This annotation will run once before the start of the first test method belonging to the same test suite.

@BeforeClass

This annotation will be triggered before the first @Test annotation of the same class. It runs only once.

@BeforeMethod

A statement which is written inside this annotation will be executed before the innovation of each method with @Test annotation tag.

@Test

The statement or method written inside this annotation is the testable test case. All other TestNG annotations get invoked with respect to @Test annotation.

@AfterMethod

A statement which is written inside this annotation will be executed after the innovation of each method with @Test annotation tag.

@AfterClass

This annotation will be triggered after the execution of all the methods of the same class. It runs only once.

@AfterSuite

This annotation will run once after the execution of all the tests belonging to the same test suite.

@AfterGroups

This annotation will be executed after the invocation of the last test method of the same group.

These are immediate TestNG annotation which is used inside the test class. Let’s discuss some other annotations which help us in developing data driven framework in Selenium WebDriver.

@Parameters

This annotation helps developers to pass the short range of test data parameters to the test.

@Factory

This is another annotation which helps to set the value to the constructor. It returns Object[].

@DataProvider

This annotation makes any method as data supplier. It returns 2D object array like Object[][]. The method which takes test data from the methods which have implemented @DataProvider annotation must use dataProvider=’name of the @DataProvider annotation’.

@Listeners

This annotation helps us to implemented advanced reporting and logging in the test framework.

Recommended: WebDriver Event Listener for Effective Logging and Reporting

We will exclusively see the application of these special annotations in separate tutorials. As of now, let’s see the program which implements basic TestNG annotations.

See the order of execution of the above program in Console screenshot below:

TestNG Annotations Console

 

Join Inviul fb group

Related Articles

Add Comment