Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 24: Annotations In TestNG – A Quick Guide | Make Selenium Easy

Posted on 12/12/2018 By admin

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 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 tag have run. If 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  or  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

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

Uncategorized

Post navigation

Previous Post: Frequently Asked Java Program 24: Java Program to Capitalize First Character of Each Word in a String Sentence | Make Selenium Easy
Next Post: TestNG Tutorials 53: DataProvider in TestNG – Is It Mandatory To Have Return Type as Object in DataProvider Method | Make Selenium Easy

Related Posts

Git Tutorial 6 – Git Add – Add Changes From Working Directory To Staging Directory Uncategorized
July 30, 2018 – Make Selenium Easy Uncategorized
GroupsTest – Make Selenium Easy Uncategorized
Postman Tutorial Part 44 – Data Driven Testing in Postman Using CSV File – Make Selenium Easy Uncategorized
TestNG Tutorials 28: Grouping Test Methods In TestNG Uncategorized
Advanced Concept of Selenium – Design Patterns in Selenium WebDriver – There is Not Only Page Object Model Design Pattern Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark