In this tutorial, we will be studying all the annotations of TestNG along with the different attributes supported.
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.
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.
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.
@Test
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.
@Test
public void sampleTest() {
//Any test logic
System.out.println("Hi! QA Earth here!");
}
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.
@Test(description = "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.
@Test(dataProvider = "name of dataProvider")
3. priority - This attribute helps in prioritizing the test methods. The default priority starts with 0 and tests execute in ascending order.
@Test(priority = 2)
4. enabled - This attribute is used to specify whether the given test method will run with the suite or class or not.
@Test(enabled = false)
5. groups - Used to specify the groups, the test method belongs to.
@Test(groups = { "sanity", "regression" })
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.
@Test(dependsOnMethods = { "dependentTestMethodName" })
8. dependsOnGroups - Used to specify 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 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.
@Test (timeOut = 500)
@BeforeSuite
The annotated method will run only once before all tests in this suite have run.
@AfterSuite
The annotated method will run only once after all tests in this suite have run.
@BeforeClass
The annotated method will run only once before the first test method in the current class is invoked.
@AfterClass
The annotated method will run only once 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 after all the test methods belonging to the classes inside the <test> tag have run.
@DataProvider
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.
//Data provider returning 2D array of 3*2 matrix @DataProvider(name = "dataProvider1") public Object[][] dataProviderMethod1() { return new Object[][] {{"krishna","reddy"}, {"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); }
@Parameter
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-
@Test (timeOut = 500)<suite name="sampleTestSuite"> <test name="sampleTest"> <parameter name="sampleParamName" value="sampleParamValue"/> <classes> <class name="TestFile" /> </classes> </test> </suite>
Sample Test Script with @Parameter annotation-
public class TestFile { @Test @Parameters("sampleParamName") public void parameterTest(String paramValue) { System.out.println("sampleParamName = " + sampleParamName); }
@Listener
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.
@Listeners(PackageName.CustomizedListenerClassName.class) public class TestClass { WebDriver driver= new FirefoxDriver();@Test public void testMethod(){ //test logic } }