TestNG Tutorials 46: Overriding Parameters in TestNG | Make Selenium Easy

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:

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);
        }
        
}

Testng xml:




        
        
        
                
                        
                
         
 

Output:

Suite level parameters values were used by test method. 

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

Testng 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:




        
        
        
        
        
                
                        
                        
                        
                
         
 

Output:

Class level parameters values were used by test method.




        
        
        
        
        
                
                        
                        
                        
                        
                                
                                        
                                        
                                                
                                                
                                
                                
                
         
 

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:




        
                
                
                        
                                
                                        
                                        
                                        
                                        
                                        
                                

                        
                
         


        
                
                
                        
                                
                                        
                                        
                                        
                                        
                                        
                                

                        
                
         
 

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

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning