List of Annotations in TestNG

Annotations

These provide information about the class / method. Which is represented by "@" prefix. Also differs in each project w.r.t the requirements. Even refered to control the flow of methods.

Each of these annotations execute at a specific event in the TestNG, which also determines the sequence of running the tests. Let's see them in detail -

@BeforeSuite - This method, run BEFORE all tests in this suite have run.
@AfterSuite - This method, run AFTER all tests in this suite have run.

@BeforeTest - This method, run BEFORE any test method belonging to the classes inside the tag is run.
@AfterTest - This method, run AFTER all the test methods belonging to the classes inside the tag have run.

@BeforeGroups: Run BEFORE the first test method that belongs to the group is invoked.
@AfterGroups: Run AFTER the last test method that belongs to the groups is invoked.

@BeforeClass - Run BEFORE the first test method in the current class is invoked.
@AfterClass - Run AFTER all the test methods in the current class have been run.

@BeforeMethod - Run BEFORE each test method.
@AfterMethod - Run AFTER each test method.

@Test - Test method.

Workflow of the Annotations :

All annotations described above are executed on runtime in the following order:

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeGroups
  • BeforeMethod
  • Test
  • AfterMethod
  • AfterGroups
  • AfterClass
  • AfterTest
  • AfterSuite

Attributes used in these annotations :

Under @Test

  1. description : This is generally used to define the test method.
    Example - @Test(description = "This is a sample test method")

  2. alwaysRun : This is used with a test method ensuring to run always even the tests fail. When the value for this is set to true this method always executes.
    Example - @Test(alwaysrun = true)

  3. dataprovider : This method mentions the name of the dataprovider for this method.
    Example - @Test(name = "testdata")

  4. dataProviderClass : It is a class to look for the data. If not mentioned, the name of the current class is taken or its base class.
    Example - @Test(dataprovider = "TestData, dataProviderClass = test.class)

  5. dependsOnGroups : A list of groups which the method depends on. When these dependent groups are executed, then the test method is executed. If they get failed, the test method is skipped.
    Example - @Test(dependsonGroups = {"group1","group2"})

  6. dependsOnMethods : A list of methods which the test method is dependent on. When these dependent methods are executed, then the test method is executed. If they get failed, the test method is skipped.
    Example - @Test(dependsOnMethods = {"method1","method2"})

  7. enabled - This attribute is used to ignore a particular test when we don't want it to be performed. All we need to do is to set the attribute to false.
    Example - @Test(eenabled = false)

  8. expectedExceptions - List of Exceptions that a test method is expected to throw. When none/different are thrown, the test is marked as a failure.
    Example - @Test(expectedEExceptions = {ArithemeticException.class})

  9. groups : Used to mention the specific group the test belongs to.
    Example - @Test(groups = {"Regression","Smoke"})

  10. invocationCount - It specifies the number of count the test method must be invoked.
    Example - @Test(invocationCount = 4)

  11. invocationTimeOut - It specifies the timeout the test method must be invoked. Ignores when the invocation count isn't mentioned.
    Example - @Test(invocationCount = 5, invocationTimeOut = 4000)

  12. priority - It specifies the priority of the test method to be executed. Lower priorities ones are executed first.
    Example - @Test(priority = 0)

  13. timeout - It is used to specify the maximum number of milliseconds for a test to be completed. If the test exceeds, the test method would terminate and is marked with a exception org.testng.internal.thread.ThreadTimeoutException.
    Example - @Test(timeout = 200)

Under the rest of the Annotations -

  1. alwaysRun - For before methods (except beforeGroups) when set to true, the configuration will run irrespective of which group it belongs.
    For after methods when set tto true, the configuration will run even though the previous methods are skipped or failed.
    Example - @before../@after..(alwaysrun = true)

  2. dependsOnGroups - A list of groups which the class/method depends on. When these dependent groups are executed, then the class/method is executed. If they get failed, then these classes/methods are skipped.
    Example - @before../@after..(dependsonGroups = {"group1","group2"})

  3. dependsOnMethods - A list of methods which the class/method is dependent on. When these dependent methods are executed, then the class/method is executed. If they get failed, then these classes/methods are skipped.
    Example - @before../@after..(dependsOnMethods = {"method1","method2"})

  4. enabled - This attribute is used to ignore a particular class/method when we don't want it to be performed. All we need to do is to set the attribute to false.
    Example - @before../@after..(enabled = false)

  5. groups - Used to mention the specific group the before/after classes/methods belong to.
    Example - @before../@after..(groups = {"Testgroup1","Testgroup2"})

  6. inheritGroups - If true, this method will belong to groups specified in the @Test annotation at the class level.

  7. onlyForGroups - Only for @BeforeMethod and @AfterMethod. If specified, then this setup/teardown method will only be invoked if the corresponding test method belongs to one of the listed groups.

Code Snippets for all the above ones are here