Home » What Is TestNG Annotations-Benefits, Hierarchy

What Is TestNG Annotations-Benefits, Hierarchy

What Is TestNG Annotations

What Is TestNG Annotations- Its Benefits, Hierarchy & TestNG Case Priorities?

Annotations generally represent a note or a comment on a diagram, etc., to convey its meaning. TestNG also uses them for the exact reason. TestNG annotations are the code written inside your source test code logic to manage the flow of the execution of tests? Therefore, it is vital to annotate your processes in TestNG to execute the tests. TestNG will ignore the process that does not contain an annotation since it won’t know when to run it.

Annotations in TestNG can be comprehended as distinct pieces of code integrated within the logic of a program to manage the sequential execution of the test methods. In other words, TestNG Annotations assist in deciding the sequence of the tests made to execute. These annotations are extracted from Java language and are a critical component of Test Next Generation (TestNG), an automation framework that Selenium uses.

A TestNG annotation begins from the symbol “@,” and the annotation name follows after that. Apart from the header file and ‘@’ symbol, you require nothing to execute TestNG annotations.

Different Kinds Of TestNG Annotations

In TestNG, there are ten kinds of annotations:

  • @BeforeSuite – In TestNG, the @BeforeSuite method executes before all other test methods.
  • @AfterSuite – In TestNG, the @AfterSuite method runs after all other test methods have been completed.
  • @BeforeTest – The @BeforeTest function in TestNG runs before all of the test methods in that folder are executed.
  • @AfterTest – TestNG’s @AfterTest function runs after all test methods in a folder are executed.
  • @BeforeClass – In TestNG, the @BeforeClass function runs before the first method collects the current class.
  • @AfterClass – In TestNG, the @AfterClass method is called after all of the current class’s test methods have been completed.
  • @BeforeMethod – In TestNG, the @BeforeMethod method executes before each test method.
  • @AfterMethod – In TestNG, the @AfterMethod method executes after each test method is run.
  • @BeforeGroups – In TestNG, the @BeforeGroups method runs before the test cases in that group run. It only executes once.
  • @AfterGroups – The @AfterGroups function in TestNG executes after the test cases in that group have been run. It is only used once.

These TestNG annotations have self-explanatory definitions. One of the foremost reasons to prefer TestNG is that it is straightforward to learn.

Benefits Of TestNG Annotations

Let’s discuss the benefits of TestNG Annotations-

  • TestNG annotations aid in the creation of a comprehensive and complete test report that includes information on the number of test cases run, passed test cases, failed test cases, and skipped test cases.
  • It enables the simple grouping of multiple test cases using the testing.xml file. Moreover, one can prioritize the execution of test cases.
  • TestNG Annotations can implement the same test case multiple times by using the keyword, ‘invocation count.’
  • Annotations in TestNG are easily understandable.
  • TestNG Annotations provide simple coding of tests to eliminate the requirement for a static primary method.
  • TestNG assists in the grouping of test cases and their execution.
  • TestNG offers the ability to prioritize test cases based on the demands of the programmer.
  • TestNG helps to execute the test cases in a parallel manner.
  • TestNG provides an ideal way of report generation according to the test results.
  • TestNG is strongly typed, which specifies the capability of solid error checking.
  • TestNG can also link to external data sources.
  • To execute test cases, there is no requirement to follow a pattern or format.
  • Additional parameters can be passed to TestNG annotations.
  • In the case of TestNG annotations, there is no requirement to extension of any test classes.
  • TestNG Annotations are highly typed, which means that errors are detected at compile time.

Hierarchy of the TestNG Annotations:

TestNG has a lot of annotations that help you write good test source code when you’re testing software. So, how does TestNG decide which test case to run first, followed by the next, and so on? In these annotations, the answer is a hierarchy. Therefore, testNG includes a hierarchy among the annotations. This hierarchy is as follows – the top being the highest priority):

Hierarchy of the TestNG Annotations
  • @BeforeSuite
  • @BeforeTest
  • @BeforeClass
  • @BeforeMethod
  • @Test
  • @AfterMethod
  • @AfterClass
  • @AfterTest
  • @AfterSuite

TestNG Prioritization

Prioritization in TestNG is a method to provide a series to the methods so that they do not execute out of order. Since alphabetically running test cases in TestNG have no logical sequence, equipping priority to these test cases helps us organize our tests’ execution.

Priority in TestNG test cases is a parameter with a characteristic value as a priority.

The priorities are an added option that we can use with the text annotations. This characteristic determines the importance of the annotation. But always remember that priority check occurs after the annotation check by TestNG. As a result, the TestNG annotation hierarchy comes first, followed by priority-based execution.

It is necessary to consider the following points concerning priority in TestNG:

  • Only @Test methods can be used to define Priority in TestNG test methods.
  • The test case method has a higher priority when the priority number is lower.
  • Priority in TestNG contains only integer values. The value can be zero, negative or positive.
  • In TestNG, if a priority is defined in decimal, it must first be converted to an integer (through typecasting).
  • One process is enabled to have only one priority in TestNG.
  • Priority cannot pass through the XML files.
TestNG Prioritization

Conclusion

TestNG Annotations are used to handle the next method to be accomplished in the test script. It represents a batch of code inserted into the program or business logic used to manage the flow of processes in the test script.

The annotations are very easy to understand and execute. There is no predefined format or rule, and the tester needs to annotate methods using their judgment. They make Selenium test scripts more sophisticated, effective, and manageable. Using them is extremely helpful for testers and makes their lives much more accessible.

Leave a Reply

Your email address will not be published.