Categories: Selenium TopicsTestNG Tutorials

TestNG Tutorials 24: Annotations In TestNG – A Quick Guide

Hello Folks,

As part of learning TestNG step by step, I will list all available annotations of TestNG in this post and will see each and every annotations in details in later posts.

As per TestNG documentation, TestNG provides you below annotations:

  1. @BeforeSuite
  2. @AfterSuite
  3. @BeforeTest
  4. @AfterTest
  5. @BeforeGroups
  6. @AfterGroups
  7. @BeforeClass
  8. @AfterClass
  9. @BeforeMethod
  10. @AfterMethod
  11. @DataProvider
  12. @Factory
  13. @Listeners
  14. @Parameters
  15. @Test

@BeforeSuite:

A suite is a collection of tests. If you want to do some setup before tests are run like generating consolidated reports of all tests, printing information about tests to be run, printing details like browser name, project name etc, you can use @BeforeSuite annotated method. The annotated method will be run before all tests in this suite have run. Suppose if a suite has 4 tests, @BeforeSuite annotated method will be run only once.

@AfterSuite:

The annotated method will be run after all tests in this suite have run. Once suite execution is done, probably you would like to send reports to your superior, or you would like to upload report  in shared path, cleanup resources etc, you can use @AfterSuite annotation method. It will be last method to be executed in a suite. So all types of cleanup activities can be done here.

@BeforeTest:

As I said above, “A suite is a collection of tests”. Now we are inside suite and just about to execute tests. The annotated method will be run before any test method belonging to the classes inside the <test> tag is run. If you would like to do some setup which is specific to tests and should be setup before test is run, can be done here. Remember, @BeforeTest and @AfterTest is unique to test levels. If a suite contains multiple tests, these annotated method will not be applicable for all tests.

@AfterTest:

As I said above, “A suite is a collection of tests”. Now we are inside suite and just after execution of a test in suite. The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have runIf you would like to do some setup which is specific to tests and should be run after test is run, can be done here. Remember, @BeforeTest and @AfterTest is unique to test levels. If a suite contains multiple tests, these annotated method will not be applicable for all tests.

@BeforeGroup:

TestNG allows you to perform sophisticated groupings of test methods. Not only can you declare that methods belong to groups, but you can also specify groups that contain other groups. Groups are specified in your testng.xml file and can be found either under the <test> or <suite> tag. This annotated method will help you do prerequisite setup before group is run. The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

@AfterGroup:

This annotated method will help you do prerequisite setup after group is run completely. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

@BeforeClass:

A test can have multiple classes and a class can have multiple test methods. The annotated method will be run before the first test method in the current class is invoked. Remember @BeforeClass and @AfterClass methods are unique to a particular class.

@AfterClass:

A test can have multiple classes and a class can have multiple test methods. The annotated method will be run after all the test methods in the current class have been run. . Remember @BeforeClass and @AfterClass methods are unique to a particular class.

@BeforeMethod:

This is at test method level. This annotated method can be used to run before a test method is run. The annotated method will be run before each test method.

@AfterMethods:

This is at test method level. This annotated method can be used to run after test method is run. The annotated method will be run after each test method.

@DataProvider

Marks a method as supplying data for a test method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

@Factory:

Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[].

@Listeners:

Defines listeners on a test class.

@Parameters:

Describes how to pass parameters to a @Test method.

@Test:

Marks a class or a method as part of the test.

You can see how TestNG provides a rich list of annotations which helps you in proper setup before or after at different levels. We will see annotations in details.

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

 

 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Recent Posts

TestNG Tutorials 64: Dependency in TestNG – ignoreMissingDependencies – Another Way of Achieving Soft Dependencies

Hello Folks, There are two types of dependencies in TestNG: 1. Hard Dependency : All the methods you depend on must have…

19 hours ago

API Testing Tutorial Part 14 – Installation of Postman Tool

Hello Folks, As part of our API Testing series, we will see “Installation of Postman Tool” in this post. Postman tool was…

19 hours ago

TestNG Tutorials 63: Dependency in TestNG – Usage of Regular Expressions with DependsOnGroup

Hello Folks, We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below:…

6 days ago

Frequently Asked Java Program 25: Java Program to Convert a String Sentence in Camel Case

"CamelCase" is a naming convention in which words are joined together without any whitespace in between and each word starts…

7 days ago

Frequently Asked Java Program 24: Java Program to Capitalize First Character of Each Word in a String Sentence

Problem Statement: Input: make selenium easy Output: Make Selenium Easy Convert first character of each word into upper case. Solution:…

7 days ago

Ways of Handling StaleElementReferenceException Without PageFactory

Hello Folks, Previously, I had published on Handling StaleElementReferenceException using PageFactory. But many people asked how to handle it if they…

2 weeks ago