TestNG Parameterization Using DataProviders | TestNG Tutorial
TestNG Parameterization Using DataProviders
Parameterized tests allow developers to run the same test over and over again using different values.
There are two ways to set these parameters:
- with testng.xml
- with Data Providers
Let’s see passing parameters using DataProviders:
Specifying parameters in testng.xml might not be sufficient if you need to pass complex parameters, or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc…). In this case, you can use a Data Provider to supply the values you need to test. A Data Provider is a method on your class that returns an array of objects. This method is annotated with @DataProvider:
Check below video to see “TestNG Parameterization using DataProviders”
Please be patient. The video will load in some time.
If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.
package softwareTestingMaterial; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderClass { // This method takes data as input parameters. The attribute dataprovider is mapped to "getData" @Test (dataProvider="getData") // Number of columns should match the number of input parameters public void loginTest(String Uid, String Pwd){ System.out.println("UserName is "+ Uid); System.out.println("Password is "+ Pwd); } //If the name is not supplied, the data provider’s name automatically defaults to the method’s name. //A data provider returns an array of objects. @DataProvider(name="getData") public Object[][] getData(){ //Object [][] data = new Object [rowCount][colCount]; Object [][] data = new Object [2][2]; data [0][0] = "FirstUid"; data [0][1] = "FirstPWD"; data[1][0] = "SecondUid"; data[1][1] = "SecondPWD"; return data; } }
Console Output:
[TestNG] Running: UserName is FirstUid Password is FirstPWD UserName is SecondUid Password is SecondPWD =============================================== softwaretestingmaterial Total tests run: 2, Failures: 0, Skips: 0 ===============================================
You could find the complete TestNG tutorial here.
If you are not regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
Very helpful site