Introduction to TestNG Annotations
As we know that TestNG is a Test Automation Framework that is used along with the Selenium framework to ease the test automation process. But the question arises what is annotation and why they are used? Annotations in TestNG are an instruction/ code which is added in the code to instruct the compiler to control the flow of test case execution according to the specific requirements. As there is a sequence of the methods, variables, and functions that need to be followed to execute the test cases properly. TestNG annotations guides this to the compiler with the simple and easy to understand annotations and hence there is no need to use the static main method in the program which specifies the start of execution of code.
There are various TestNG annotations and each annotation is used for a different purpose. Annotations with their explanations are mentioned below:
1. @BeforeSuite: This annotated method is run before all the tests in a test suite is run.
2. @AfterSuite: This annotated method is run after all the tests in a particular test suite is run.
3. @BeforeTest: This annotated method is run before any test method related to the class which is defined inside the <test> tag.
4. @AfterTest: This annotated method is run after every test method related to the class which is defined inside the <test> tag is executed.
4.5 (2,672 ratings)
5. @BeforeClass: This annotated method is run only once and is executed before the first test method of the mentioned class.
6. @AfterClass: This annotated method is run only once and is executed after all the test methods of the mentioned/ current class is executed.
7. @Test: This annotated method marks any class or method as a part of a test that needs to be tested. It has various parameters which are mentioned below:
- alwaysRun: The test method always run if this is set to true no matter the test case on which it depends gets failed.
- dataProvider: This defines the name of the data provider for the test case.
- depends on methods: This defines the list of methods on which that particular method depends on.
- dependsOnGroups: This defines the list of groups on which that particular method depends on.
- Description: It defines the description of the method
- Enabled: It defines whether the method is enabled or not. If it is set to false, then that method is not executed or is considered as not present.
- Priority: It defines the priority of the particular test method following which it should be executed. Lower Priority values are scheduled first and if the test cases are not defined any priority values, then the TestNG assigns a default priority value (0).
- successPercentage: It defines the percentage of success which is expected from that method.
- invocationCount: As the name suggests, it specifies the number of times that the method is called.
- timeOut: It defines the maximum number of times that a particular test case should take to execute. It is defined in milliseconds.
- InvocationTimeOut: Like timeOut, it specifies the number of milliseconds that a method should take to complete all the invocation counts. Its values are always provided in milliseconds.
8. @BeforeMethod: This annotated method runs before each and every test method.
9. @AfterMethod: This annotated method will run after each and every test method.
10. @BeforeGroups: This annotated method will run before the first test method of the respective group which is a part of the execution.
11. @AfterGroups: This annotated method will run after all the test methods or after the last method of the respective group is invoked which is a part of the execution.
12. @DataProvider: This annotated method is used to supply the data to the test method. It can be used with any @test method in order to supply the input. It is used to supply the complex parameters to the @test methods which cannot be provided in the XML file. Every @DataProvider must return Object[ ] [ ] where each Object[ ] is assigned the list of parameters of test method. It has an attribute as ‘name’ which needs to be provided, if it’s not mentioned by the programmer, then its value is set to the name of the method. It has one attribute with the name ‘parallel’ which is by default ‘false’. If set to true, then all test cases
13. @Listeners: This annotation is used to define the listeners in a test method.
14. @Parameters: This annotation is used to define the parameters in a test method. ‘Value’ attribute is used containing all the variable names that are used as parameters.
15. @Factory: This annotation is used to mark any test method as a factory and returns the object (Object[ ]) that is used as TestNG classes by the TestNG file.
Advantages of TestNG Annotations
Below given are some of the advantages of TestNG annotations:
- TestNG is a framework used with the Selenium having annotations that provide an easy and simple to use way to control the flow of execution of automated test cases.
- TestNG helps to run/ execute the test cases in a parallel manner.
- TestNG provides the facility of test case prioritization according to the requirements of the programmer.
- TestNG helps in the grouping of test cases and executing them accordingly.
- TestNG provides an excellent way of report generation according to the test results.
- TestNG is strongly typed which defines the capability of strong error checking.
- It provides an easy way of parameter passing and providing inputs to the test cases.
- TestNG can also connect to external data sources.
- There is no need for a pattern or format that needs to be followed for the execution of test cases. Annotations provide the facility to call the methods according to the specific requirements.
- TestNG generates HTML based reports representing the data in a graphical format which is easy and more convenient to understand.
The above description clearly explains the various annotations of TestNG and the purpose of each annotation in controlling the flow. Though the annotations of TestNG are very easy to use and we can use it according to the requirements but a deep understanding of each annotation should be gathered before using them in the test automation.
This is a guide to TestNG Annotations. Here we discuss various TestNG annotations with advantages. You can also go through our other related articles to learn more-