TestNG Annotations

TestNG uses annotations to help developers to write the tests. Let us learn a few important testng annotations and their lifecycle.

1. TestNG Annotations

The following is a table containing information about all the annotations provided by TestNG and a brief description of them. The source code of all the annotations can be found in the Git repository.

AnnotationDescription
@BeforeSuiteThe annotated method will be executed before any tests are declared inside a TestNG suite.
@AfterSuiteThe annotated method will be executed after any tests are declared inside a TestNG suite.
@BeforeTestThe annotated methods will be executed before each test section is declared inside a TestNG suite.
@AfterTestThe annotated methods will be executed after each test section is declared inside a TestNG suite.
@BeforeGroupsBeforeGroups annotated method will run before any of the test methods of the specified group is executed.
@AfterGroupsAfterGroups annotated method will run after any of the test methods of the specified group gets executed.
@BeforeClassBeforeClass annotated method is executed before any of the test methods of a test class.
@AfterClassAfterClass annotated method is executed after the execution of every test method of a test class are executed.
@BeforeMethodThese annotated methods are executed before the execution of each test method.
@AfterMethodThese annotated methods are executed after the execution of each test method.
@DataProviderMarks a method as a data-providing method for a test method. The said method has to return an Object double array (Object[ ][ ]) as data.
@FactoryMarks an annotated method as a factory that returns an array of class objects (Object[ ]). These class objects will then be used as test classes by TestNG. This is used to run a set of test cases with different values.
@ListenersApplied on a test class. Defines an array of test listener classes extending org.testng.ITestNGListener. Helps in tracking the execution status and logging purpose.
@ParametersThis annotation is used to pass parameters to a test method. These parameter values are provided using the testng.xml configuration file at runtime.
@TestMarks a class or a method as a test method. If used at the class level, all the public methods of a class will be considered as a test methods even if they are not annotated.

Refer to this table of TestNG Annotations whenever you are in confusion.

2. Test Lifecycle

Let us see the sequence in which the methods annotated with the above annotations get executed.

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeMethod
  5. @Test
  6. @AfterMethod
  7. @AfterClass
  8. @AfterTest
  9. @AfterSuite

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode