TestNG testing framework — Parameterization
Arvind Choudhary
Posted on May 11, 2022
What is Parameterization?
Whenever we define any parameters on any of our methods, it means we are parametrizing that method. e.g. assuming areaOfSquare(int side);takes one argument which is integer and returns ares of square. it also allows you to use the same method for N. no of values.
@test method of TestNG supports parameterization, which in simple terms means it allows us to pass values to our test method at runtime.
Ways to Parameterize @test Method
- @DataProvider
- @Parameters
Parameterization with @DataProvider annotation
Always remember to use below 2 if plan on parameterizing your @test with data provider.
- @DataProvider annotation with name attribute.
- @test with attribute dataProvider having same value as specified for above step.
Code:
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderDemo {
/**
* always use name attribute for your dataProvider(), you can have
* N. no of dataprovider in your Test class and with the help of dataprovider attribute of @Test(refer to line 57)
* you can specify which dataprovider method to use.
*/
// always returns 2D Object array
@DataProvider(name = "testData")
public Object[][] dataProvider(){
// 2D Object array initialized
Object[][] objectArray = new Object[2][2];
/**
* 2D Array structure
* | abc | cde |
* | efg | hij |
*
* what @data provider will do here is that it will send data of one row at a time to test method,
* then next row and so on.
*
* no. of rows = no. of times your @Test will get executed
*/
// adding value in objectArray
objectArray[0][0] = "abc";
objectArray[0][1] = "cde";
objectArray[1][0] = "efg";
objectArray[1][1] = "hij";
// return 2D Object array
return objectArray;
}
/**
* make sure to use dataProvider attribute on your @Test annotation(refer to line 57),
*
* Also change you method signature according to what you are passing from your dataProvider,
* if you are not sure on what you should do then you can also keep your parameter type as String &
* testng would typecase it for you to string.
*
* no. of parameters on @test method should be same as no. of Object defined on your object array(column-wise).
* ref line 19, we have 2 columns and 2 rows. so you should have 2 parmaters(2 columns) in your method signature.
* (ref line no. 58)
* -------------------------OR---------------------------
*
* use (String... args), here dont have to specify each parameter,
* you will get one array which is args and you can work with it with the help of index,
* each parameter will be stored on one index
* assuming current case your array will have 2 indexes [0 & 1]
* where 0th index would hold first value and 1st index would hold second value.
* (ref line no. 63)
*/
@Test(dataProvider = "testData") // line 57
public void testMethod(String val1, String val2){ //line 58
System.out.println("testMethod called with ["+val1+", "+val2+"]");
}
@Test(dataProvider = "testData")
public void testMethod1(String... args){ //line 63
System.out.println("testMethod1 called with ["+args[0]+", "+args[1]+"]");
}
}
Output:
testMethod called with [abc, cde]
testMethod called with [efg, hij]
testMethod1 called with [abc, cde]
testMethod1 called with [efg, hij]
Parameterization with @Parameters annotation
Passing value with @Parameters is bit tricky here as we will be passing values from testng.xml
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test1">
<parameter name="value1" value="abc"></parameter>
<parameter name="value2" value="cde"></parameter>
<parameter name="value3" value="klm"></parameter>
<classes>
<class name="ParametersDemo" ></class>
</classes>
</test> <!-- Test -->
<test name="Test2">
<parameter name="value1" value="efg"></parameter>
<parameter name="value2" value="hij"></parameter>
<classes>
<class name="ParametersDemo" ></class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Script
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParametersDemo {
/**
*
* in @Parameters you should specify parameter name
* and same should be present in testng.xml.
* refer to line 5 & 13 of testng.xml
*
* @param val1 parameter should be specified in test.xml
* @param val2 parameter should be specified in test.xml
* @param val3 is Optional, if specified well it would take specified value else defualt value
* refer to line 15, we have use @Optional
*/
@Test
@Parameters({"value1","value2","value3"})
public void testMethod(String val1,String val2,@Optional("default") String val3){
System.out.println("testMethod called with ["+val1+","+val2+","+val3+"]");
}
}
Output:
testMethod called with [abc,cde,klm]
testMethod called with [efg,hij,default]
As you see above for second test tag we only specified 2 parameters instead of 3 and because of @Optional usage we didn’t get any error.
if you would have not specified @Optional and you didn’t specify parameter in testng.xml you will below error.
testMethod called with [abc,cde,klm]
org.testng.TestNGException:
Parameter 'value3' is required by @Test on method testMethod but has not been marked @Optional or defined
Useful URLs:
Javatpoint parameters
Posted on May 11, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.