TestNG testing framework — Parameterization

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 you 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:
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
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:
I’ve covered control annotations here @ TestNG testing framework — Annotations
Thank you for staying along, would appreciate if you could drop review based on what went well and what could have been focused more.