TestNG Annotations

Posted By :Nidhi Ojha |30th December 2019

In this blog, we will be studying all the annotations of TestNG along with the different attributes supported. An annotation is a tag or metadata that gives additional information about a class, interface, or method. TestNG make use of these annotations to give several features that aid in the creation of a robust testing framework. Here, we will refer to each TestNG annotation in detail and learn its syntax and usage.

@Test:

The @Test is the very important and commonly used annotation of TestNG. It is used to spot a method as Test. So, any method overhead we see @Test annotation is considered as a TestNG test.

 

@Test

public void sampleTest() {  

   //Any test logic

   System.out.println("Hi! Oodles Technlogies!");  

}

 

Now, let’s see some major attributes of @Test annotations-

1. description: The ‘description’ attribute is used to describe the test method. It generally contains a one-liner test summary.

@Test(description = "Test summary")

2. dataProvider: This attribute supports creating data-driven tests. It is used to identify the name of the data provider for the test.

@Test(dataProvider = "name of dataProvider")

3. priority: This attribute supports prioritizing the test methods. The default priority begins with 0 and tests execute in ascending order.

@Test(priority = 2)

4. enabled:  This attribute is used to identify whether the given test method will run with the suite or class or not.

@Test(enabled = false)

5. groups:  Used to identify the groups, the test method belongs to.

@Test(groups = { "sanity", "regression" })

7. dependsOnMethods:  Used to identify the methods on which the test method depends. The test method only runs succeding successful execution of the dependent tests.

@Test(dependsOnMethods = { "dependentTestMethodName" })

8. dependsOnGroups: Used to identify the groups on which the test method depends.

@Test(dependsOnGroups = { "dependentGroup" })

9. alwaysRun:  When set as True, the test method runs even if the dependent methods fail.

@Test(alwaysRun=True)

10. timeOut:  This is used to identify a timeout value for the test(in milliseconds). If the test takes greater than the timeout value identified, the test terminates and is marked as a failure.

@Test (timeOut = 500)

 

@BeforeSuite:

The annotated method will run a single time before all tests in this suite have run.

@AfterSuite:

The annotated method will run a single time after all tests in this suite have run.

@BeforeClass:

The annotated method will run a single time before the first test method in the current class is invoked.

@AfterClass:

The annotated method will run a single time after all the test methods in the current class have been run.

@BeforeTest:

The annotated method will run before any test method belonging to the classes inside the <test> tag is run.

@AfterTest:

The annotated method will run following all the test methods belonging to the classes inside the <test> tag have run.

@DataProvider: 

Using @DataProvider we will be able to create a data-driven framework in which data is passed to the associated test method and multiple iterations of the test run for the different test data values passed from the @DataProvider method. The method annotated with @DataProvider annotation return a 2D array or an object.

//Data provider returning the 2D array of 3*2 matrix

@DataProvider(name = "dataProvider1")

   public Object[][] dataProviderMethod1() {

      return new Object[][] {{"Kuldeep","Rana"}, {"k1","r1"},{"k2","r2"}};

   }

 

   //This method is bound to the above data provider

   //The test case will run 3 times with different set of values

   @Test(dataProvider = "dataProvider1")

   public void sampleTest(String str1, String str2) {

      System.out.println(str1 + " " + str2);

   }


About Author

Nidhi Ojha

Nidhi is certified in Manual Testing and SQL. She has done B.tech in Electronics and Communication branch, apart from this she loves to travel to different places.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us