TestNG Tutorials 46: Overriding Parameters in TestNG

Hello Folks,

In this post we will learn an important concept of parameters in TestNG.

We know that we can create parameterised methods in a TestNG class and pass parameters to these methods through testng xml. We can pass parameters at four levels:

  1. At suite level
  2. At test level
  3. At class level
  4. At method level

 

Parameter value at higher level will be overridden by value at lower level if same parameter is passed. For example: If we pass a parameter say “Param” with value say “value1” at suite level and also we pass value “value2” for same parameter “Param” at test level, then parameter “Param” will have value as “value2” at that test level.

We will see some examples below:

Scenario 1: Passing parameters at suite level:-

TestNG class:

[java]package Parameters;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParameterTest {
@Test
@Parameters({"testParameters1","testParameters2"})
public void testMethod(String testParameters1, String testParameters2)
{
System.out.println("Paramters one for test method: "+testParameters1);
System.out.println("Paramters two for test method: "+testParameters2);
}

}
[/java]

Testng xml:

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="testParameters1" value="testParameters 1 value" />
<parameter name="testParameters2" value="testParameters 2 value" />
<test thread-count="5" name="Test">
<classes>
<class name="Parameters.ParameterTest"></class>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>
[/xml]

Output:

Suite level parameters values were used by test method. 

Scenario 2: Passing parameters at both suite level and test level :-

Testng xml:

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="testParameters1" value="testParameters 1 value" />
<parameter name="testParameters2" value="testParameters 2 value" />
<test thread-count="5" name="Test">
<parameter name="testParameters1" value="testParameters 1 test level value" />
<parameter name="testParameters2" value="testParameters 2 test level value" />
<classes>
<class name="Parameters.ParameterTest"></class>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>
[/xml]

Note here that I am passing parameters values at both levels: Suite and test.

Output:

Parameters values at test level was used by test method. Value given at suite level was ignored.

Scenario 3: Passing parameters at suite level , test level  and class level:-

Testng xml:

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="testParameters1" value="testParameters 1 value" />
<parameter name="testParameters2" value="testParameters 2 value" />
<test thread-count="5" name="Test">
<parameter name="testParameters1" value="testParameters 1 test level value" />
<parameter name="testParameters2" value="testParameters 2 test level value" />
<classes>
<class name="Parameters.ParameterTest"></class>
<parameter name="testParameters1" value="testParameters 1 class level value" />
<parameter name="testParameters2" value="testParameters 2 class level value" />
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

[/xml]

Output:

Class level parameters values were used by test method.

Scenario 4: Passing parameters at suite level , test level, class level and method level:-

 

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="testParameters1" value="testParameters 1 value" />
<parameter name="testParameters2" value="testParameters 2 value" />
<test thread-count="5" name="Test">
<parameter name="testParameters1" value="testParameters 1 test level value" />
<parameter name="testParameters2" value="testParameters 2 test level value" />
<classes>
<class name="Parameters.ParameterTest">
<parameter name="testParameters1" value="testParameters 1 class level value" />
<parameter name="testParameters2" value="testParameters 2 class level value" />
<methods>
<!– Including individual method –>
<include name="testMethod"></include>
<!– Passing parameters for first method–>
<parameter name="testParameters1" value="testParameters 1 method level value" />
<parameter name="testParameters2" value="testParameters 2 method level value" />
</methods>
</class>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>
[/xml]

Output:

Parameter values at method level were used by test methods ignoring all other higher levels.

Conclusion:

You can notice in all scenarios that parameter value at lower level was used by test methods. Now you will be thinking where we can use this concept?

  1. You can reuse parameters. You can pass different values to same parameters for two different test.
  2. Suppose you have 10 test methods and want to execute first 5 in chrome and last 5 in firefox. You can divide then in two tests and pass browser parameters with browser values.

Example:

[xml]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test1">
<span style="color: #008000;"><parameter name="browser" value="chrome" /></span>
<classes>
<class name="Parameters.ParameterTest">
<methods>
<include name="testMethod1"></include>
<include name="testMethod2"></include>
<include name="testMethod3"></include>
<include name="testMethod4"></include>
<include name="testMethod5"></include>
</methods>

</class>
</classes>
</test> <!– Test –>

<test thread-count="5" name="Test2">
<span style="color: #008000;"><parameter name="browser" value="firefox" /></span>
<classes>
<class name="Parameters.ParameterTest">
<methods>
<include name="testMethod6"></include>
<include name="testMethod7"></include>
<include name="testMethod8"></include>
<include name="testMethod9"></include>
<include name="testMethod10"></include>
</methods>

</class>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

[/xml]

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

Leave a Reply

Your email address will not be published. Required fields are marked *