TestNG testing framework — Parameterization

Arvind Choudhary
2 min readMay 11, 2022

--

TestNG

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

  1. @DataProvider
  2. @Parameters

Parameterization with @DataProvider annotation

Always remember to use below 2 if you plan on parameterizing your @Test with data provider.

  1. @DataProvider annotation with name attribute
  2. @Test with attribute dataProvider having same value as specified for above step.

Code:

Example

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

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.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response