It took me some time to get a hang of what the TestNG DataProvider could do…Once I got to know about how it functions believe me I created wonders with it
What is a TestNG DataProvider?
There are loads of definitions on the web for it but if you are an average-but-wanting-to-learn software engineer then in simple words, a TestNG DataProvider is a Test Data Provider class…Simple! It is powerful enough to provide data according to the method which is initiating or rather calling it…
When/Why do I need a TestNG DataProvider?
Of course, most of us would agree that we provide the test data through various forms like an xml, excel, csv etc…The reason I found this feature useful when I knew the input before hand and it varied based on the test case…For example, if I am testing buying of a product on a retail website…I want to test buying of a new product, buying of a used product, buying of multiple products…I have the test data in all the three cases but then the test data differs with each test case…In this case, I can use a DataProvider…This is just one case, we can use it extensively to provide various test data…but then what I see is that we will have to hard code or give fixed input to the test each time…if we wanted to change the test data we would have to change our code…So use it as your discretion…
How to use a TestNG DataProvider?
Use a data provider Method in the same test class
Defining the Method // This is how I would define a DataProvider in TestNG – With a name – Please note that this method exists in the Test Class itself! @DataProvider(name = "getProductTestData") // Create the method which generates the test data based on a criteria – Here the criteria is by the calling method name…which means based on the method calling the dataprovider, the test data is generated public static Object[][] createProductTestData(Method m) { // Return this Product ID if the test is for a new product if (m.getName().equalsIgnoreCase("testNewProductBuy")) { return new Object[][] { new Object[] { strNewProdID } }; } // Return this Product ID if the test is for a used product else if (m.getName().equalsIgnoreCase("testUsedProductBuy")) { return new Object[][] { new Object[] { strUsedProdID} }; } // Return this Product ID if the test is for multiple products else if (m.getName().equalsIgnoreCase("testMultipleProductBuy")) { return new Object[][] { new Object[] { listOfProductIDs} }; } else // By Default the function would return an ID which is a new Product return new Object[][] { new Object[] {strNewProdID } }; } |
Calling the DataProvider @Test(groups = { "someGroup" }, enabled = true, dataProvider = "getProductTestData") public void testNewProductBuy (String strAsin) throws Exception { // Perform some actions } To explain the @Test Annotation given here, the method “testNewProductBuy” is identified as a TestNG test method which belongs to the testng group “someGroup”. As it has its “enabled” property set to “true” it would run when we run the suite. The method is also using a dataprovider “getProductTestData”. So when we run the test it would run against the product id strNewProdID which we get from the data provider… |
- Use a data provider method in a separate data provider class
Defining the Class & the Method
public class DataProviderUtlity { // This is how I would define a DataProvider in TestNG – With a name! @DataProvider(name = "getProductTestData") // Create the method which generates the test data based on a criteria – Here the criteria is by the calling method name…which means based on the method calling the dataprovider, the test data is generated public static Object[][] createProductTestData(Method m) { // Return this Product ID if the test is for a new product if (m.getName().equalsIgnoreCase("testNewProductBuy")) { return new Object[][] { new Object[] { strNewProdID } }; } // Return this Product ID if the test is for a used product else if (m.getName().equalsIgnoreCase("testUsedProductBuy")) { return new Object[][] { new Object[] { strUsedProdID} }; } // Return this Product ID if the test is for multiple products else if (m.getName().equalsIgnoreCase("testMultipleProductBuy")) { return new Object[][] { new Object[] { listOfProductIDs} }; } else // By Default the function would return an ID which is a new Product return new Object[][] { new Object[] {strNewProdID } }; } } |
Calling the DataProvider @Test(groups = { "someGroup" }, enabled = true, dataProvider = "getProductTestData",dataProviderClass = DataProviderUtlity.class) public void testNewProductBuy (String strAsin) throws Exception { // Perform some actions } To explain the @Test Annotation given here, the method “testNewProductBuy” is identified as a TestNG test method which belongs to the testng group “someGroup”. As it has its “enabled” property set to “true” it would run when we run the suite. The method is also using a dataprovider “getProductTestData” which exists in the DataProviderUtility.java class. So when we run the test it would run against the product id strNewProdID which we get from the data provider… |
Useful Links I followed to learn about this were:
- http://testng.org/doc/documentation-main.html#parameters
- http://stackoverflow.com/questions/666477/possible-to-pass-parameters-to-testng-dataprovider
- http://www.lysergicjava.com/?p=165
- http://www.mkyong.com/unittest/testng-tutorial-6-parameterized-test/
Hope this helps!!!
Comments