“DataProvider” is an important feature provided by TestNG to provide required test data to test methods to run on. We have learnt different approaches which we can utilize to get best of DataProvider in previous posts.
Let’s learn another way to make more use of DataProvider in TestNG.
Suppose we have three Test methods and each test method requires different data sets to execute. The simplest way to achieve this is by creating a separate DataProvider method for each Test method which means you require three DataProvider method for 3 Test methods. But it will become complex to manage when we have a big count of Test methods.
Solution is Parameterized DataProvider method.
We can create parameterized DataProvider method in such a way that it will return required data set as per Test method name. Let’s learn how.
We can pass a parameter of type Method which will be the actually currently running Test method and based on name of Test method we can decide what data need to be retuned. We can use switch concept to accommodate more number of test methods. DataProvider method with return type as an Object[] will help us to return different set of data as well. You can use Iterator return type as well. Based on your requirement you can decide it.
As per TestNG official document:
If you declare your @DataProvider as taking a java.lang.reflect.Method as first parameter, TestNG will pass the current test method for this first parameter. This is particularly useful when several test methods use the same @DataProvider and you want it to return different values depending on which test method it is supplying data for.
Example Program:
package DataProvider; import java.lang.reflect.Method; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; // Data class class India { public String getCapital() { return "New Delhi"; } } class USA { public String getCapital() { return "New York"; } } class UK { public String getCapital() { return "London"; } } public class CustomDataProvider { // A data provider method which will return a required data based on Test method // name @DataProvider(name = "DataContainer") public static Object[] myDataProvider(Method m) { // Matching test method name // You can use switch as well. if (m.getName().equals("India")) { India i[] = new India[1]; i[0] = new India(); return i; } else if (m.getName().equals("USA")) { USA i[] = new USA[1]; i[0] = new USA(); return i; } else if (m.getName().equals("UK")) { UK i[] = new UK[1]; i[0] = new UK(); return i; } else { System.out.println("No data found"); //System.exit(0); return null; } } // A test method @Test(dataProvider = "DataContainer") public void India(India capitalName) { System.out.println("Capital of India is " + capitalName.getCapital()); } // A test method @Test(dataProvider = "DataContainer") public void USA(USA capitalName) { System.out.println("Capital of USA is " + capitalName.getCapital()); } // A test method @Test(dataProvider = "DataContainer") public void UK(UK capitalName) { System.out.println("Capital of UK is " + capitalName.getCapital()); } }
Output:
[java][RemoteTestNG] detected TestNG version 6.14.2
Capital of India is New Delhi
Capital of UK is London
Capital of USA is New York
PASSED: India(DataProvider.India@21213b92)
PASSED: UK(DataProvider.UK@6f1fba17)
PASSED: USA(DataProvider.USA@67784306)
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
[/java]
Hope, you can implement above logic and leverage more advantages of DataProvider methods.
More about TestNG in upcoming posts. Stay tuned.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium
I try to execute the test, but show me this error:
Data Provider public static java.lang.Object[] DataProvider.CustomDataProvider.myDataProvider(java.lang.reflect.Method) must return either Object[][] or Iterator[], not class [Ljava.lang.Object;
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:137)
at org.testng.internal.Parameters.handleParameters(Parameters.java:426)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
the error due to TestNG version, I used the 6.8