TestNG Annotations Explained: Harnessing the Power of Annotations for Successful Test Automation

TestNG-2

TestNG is a popular Java testing framework used for automating and testing Java applications. TestNG makes testing more organized and efficient, thanks to its built-in support for annotations. Annotations help in organizing test methods and controlling the execution of tests, making it easier to manage and maintain test suites. This article aims to explain the TestNG annotations and how they can be used for successful test automation.

Contents

TestNG Annotation:

TestNG annotation is basically a set of metadata or in simple terms a piece of code that is added to the actual source code or business logic. These annotations help the user control the flow of execution of methods and makes the management and maintainability of the tests easier.

Commonly Used TestNG Annotations:

Here is the list of most commonly used TestNG annotations along with their hierarchy:

  1. @BeforeSuite : A method with this annotation will only run once before all the tests in the suite have run.
  2. @BeforeTest : A method with this annotation will run before any method test method belonging to class in the <test> tag in xml file.
  3. @BeforeClass : A method with this annotation will only run once before any method in the class gets executed.
  4. @BeforeMethod : A method with this annotation will run before every test method marked by @Test method.
  5. @Test : A method with this annotation will mark the method as a test method.
  6. @AfterMethod : A method with this annotation will run after every test method marked by @Test method.
  7. @AfterClass : A method with this annotation will run once after all test methods in the class gets executed.
  8. @AfterTest : A method with this annotation will run after all test methods belonging to class in the <test> tag in xml file.
  9. @AfterSuite : A method with this annotation will after all the tests in the suite have run.
  10. @DataProvider: This annotation is used to pass data to the methods. The test method gets called according to the number of rows of data provided using the data provider method.

TestNG Xml File:

Once the TestNG class files are created using the annotations and methods. It is time to create the TestNG Xml file.

The Steps to create a TestNG Xml file are shown in the picture below:

Once the TestNG Xml file has been created this is how it looks as shown in the picture below:

TestNG Xml File

Configuring TestNG Methods:

Here are some of the Real Time Use Cases or Scenarios where we are extensively using TestNG to configure and alter the flow of execution of methods:

Priority : In Real time there are situations where we need to execute our test cases in a certain sequence one after the other in a particular order .For these scenarios we make use of the priority keyword with @Test Annotation.

Priority

From the above picture it can be concluded that since for test method B priority is marked as 1 it has executed before test method A. The Default priority is marked as 0.

Multiple Execution: In Real time there are situations where we need to execute our test cases multiple times. For these scenarios we make use of the invocationCount keyword with @Test Annotation.

Invocation Count

From the above picture it can be concluded that since for test method A has been marked with the Keyword invocationCount=5 keyword with @Test Annotation so in the output window the test method A ran for 5 times.

TimeOut: In Real time there are situations where we need to fail our test cases after certain period of time . For these scenarios we make use of the timeout keyword with @Test Annotation.

timeOut

From the above picture it can be concluded that since for test method A has been marked with the Keyword timeOut keyword with @Test Annotation so in the output window the test method A failed since we gave a timeout of 5 sec but there is a wait for 6 seconds .

Depend on Methods: In Real time there are situations where one test method should run only depending on success or failure of execution of another method . For these scenarios we make use of the dependsOnMethods keyword with @Test Annotation.

depends On Methods

From the above picture it can be concluded that since for test method A has been marked with the Keyword timeOut keyword with @Test Annotation so in the output window the test method A failed since we gave a timeout of 5 sec but there is a wait for 6 seconds  and we can observe that since test method method B has been marked with keyword dependsOnMethods with @Test Annotation and in turn depending on test method A . Because of failure of test method A , test method B has been skipped.

In this article, we have explained the different TestNG annotations and their uses.By using TestNG annotations effectively, testers can ensure that their test suites are well-organized, efficient, and maintainable. With TestNG, annotations make it possible to achieve successful test automation and ensure that the application under test is thoroughly tested and meets the desired quality standards.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.