Home > Automation Testing > How to use Annotations in TestNG Framework

How to use Annotations in TestNG Framework

This post was most recently updated on July 31st, 2024

Annotations in TestNG

Annotation is a feature introduced in Java 5 and is used to add a piece of code to the java source code. 
Annotations are easy to use, maintain and understand.
Annotations provide many useful attributes to use in the code.
It is easier to find where the error occurred in the code with the use of Annotations.
TestNG Annotations made the life of testers very easy. Based on your requirements, you can access the test methods, i.e., it has no predefined pattern or format.
Parallel, multiple and specific executions of test cases is possible with Annotations.

TestNG Annotations Groups and Hierarchy 

Image: TestNG Annotations Hierarchy

In this section, we will explain the order of execution procedure in the TestNG.

Now let’s create a TestNG file as “TestNG.xml” to execute the above code.

Let’s check the output:

Let’s discuss what happens in the above code,
First BeforeSuite() method will execute as it executes before all the methods.
Eventually, AfterMethod will execute after all the methods and for each test present in the class but before executing the test case. That’s why Aftermethod is executing after each test case.
Then annotations BeforeTest(), BeforeClass(), AfterClass(), and AfterTest() methods respectively of the order mentioned are executed only once.
AfterSuite will always execute after all the methods and only once.
The following is a table containing information about all the annotations provided by TestNG and a description of them:

Annotation Description

@BeforeSuite 

This annotated method will be executed before any tests declared inside a TestNG suite.
@AfterSuite This annotated method will be executed after any tests declared inside a TestNG suite.
@BeforeTest This annotated method will be executed before each test section declared inside a TestNG suite.
@AfterTest This annotated method will be executed after each test section declared inside a TestNG suite.
@BeforeGroup BeforeGroups annotated method will run before the first test method run from the specified group.
@AfterGroup AfterGroups annotated method will run after all the test method from the specified group.
@BeforeClass This annotated method will execute before any of the test methods of a test class.
@AfterClass This annotated method will execute after the execution of every test methods of a test class is executed.
@BeforeMethod This annotated method is executed before the execution of each test method.
@AfterMethod This annotated method will execute before the execution of each test method.
@DataProvider DataProvider is used to provide any data for parameterization.
@Factory The factory is used to run a set of test cases with different values.
@Listeners Listeners are used in tracking the execution status and logging purpose.
@Parameters Parameters are used to pass parameters to a test method. These parameter values are provided using the testng.xml configuration file at runtime.
@Test A class or method is considered as a test method. If used at class level, all public methods of a class will be considered as a test method.

In the next topic, we will discuss TestNG Annotations in depth.

This Article is TAGGED in , , , , . BOOKMARK THE permalink.

Devanand Tripathi
Software Test Engineer Skills : Selenium Webdriver, Manual Testing, Database Testing, STLC Java, UI / UX Testing

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">