In this tutorial, we will be studying all the annotations of TestNG along with the different attributes supported. An annotation is a tag or metadata that provides additional information about a class, interface or method. TestNG make use of these annotations to provide several features that aid in creation of robust testing framework. Here, we will refer to each TestNG annotation in detail and study their syntax and usage.
The @Test is the most important and commonly used annotation of TestNG. It is used to mark a method as Test. So, any method over which we see @Test annotation, is considered as a TestNG test.
Now, let's see some important attributes of @Test annotations-
1. description - The 'description' attribute is used to provide a description to the test method. It generally contains a one-liner test summary.
2. dataProvider - This attribute helps in creating a data driven tests. It is used to specify the name of the data provider for the test.
3. priority - This attribute helps in prioritizing the test methods. The default priority starts with 0 and tests execute in ascending order.
4. enabled - This attribute is used to specify whether the given test method will run with the suite or class or not.
5. groups - Used to specify the groups, the test method belongs to.
7. dependsOnMethods - Used to specify the methods on which the test method depends. The test method only runs after successful execution of the dependent tests.
8. dependsOnGroups - Used to specify the groups on which the test method depends.
9. alwaysRun - When set as True, the test method runs even if the dependent methods fail.
10. timeOut - This is used to specify a timeout value for the test(in milli seconds). If test takes more than the timeout value specified, the test terminates and is marked as failure.
The annotated method will run only once before all tests in this suite have run.
The annotated method will run only once after all tests in this suite have run.
The annotated method will run only once before the first test method in the current class is invoked.
The annotated method will run only once after all the test methods in the current class have been run.
The annotated method will run before any test method belonging to the classes inside the <test> tag is run.
The annotated method will run after all the test methods belonging to the classes inside the <test> tag have run.
Using @DataProvider we can create a data driven framework in which data is passed to the associated test method and multiple iteration of the test runs for the different test data values passed from the @DataProvider method. The method annotated with @DataProvider annotation return a 2D array or object.
The @Parameter tag is used to pass parameters to the test scripts. The value of the @Parameter tag can be passed through testng.xml file.
Sample testng.xml with parameter tag-
Sample Test Script with @Parameter annotation-
TestNG provides us different kind of listners using which we can perform some action in case an event has triggered. Usually testNG listeners are used for configuring reports and logging.
The @Factory annotation helps in dynamic execution of test cases. Using @Factory annotation, we can pass parameters to the whole test class at run time. The parameters passed can be used by one or more test methods of that class.
In the below example, the test class TestFactory will run the test method in TestClass with different set of parameters - 'k1' and 'k2'.