TestNG Tutorials 45: Passing Parameters at Test Method Level in TestNG | Make Selenium Easy

Hello folks,

TestNG allows us to create parameterized methods in a TestNG class. A TestNG class may contain more than one @Test annotated methods and every test method may accept zero or more parameters. We can pass those parameters as a whole at “suite” level or “test” level.

Sometimes, it becomes confusing when there are a large number of parameters. It becomes difficult to manage and  identify related parameters belong to test methods.

Example:




        
                
                
                
                
                
                
                
                
                
                
                
                
                
                        
                
         
 

Bulky and confusing. Isn’t it??

Do you know that we can pass parameters at individual method levels as well. It helps us to keep parameters information near to method to which it belong. But it is slightly different from passing at suite and test level.

Let’s learn it now.

A sample TestNG class:

package Parameters;

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

public class ParametersAtMethodLevel {

        /*
         * Two test methods accepting parameters/arguments
         */
        @Parameters({"testParameters1","testParameters2"})
        @Test
        public void testMethod1(String testParameters1, String testParameters2)
        {
                System.out.println("Paramters one for test method 1: "+testParameters1);
                System.out.println("Paramters two for test method 1: "+testParameters2);
        }
        
        @Parameters({"testParameters3","testParameters4"})
        @Test
        public void testMethod2(String testParameters1, String testParameters2)
        {
                System.out.println("Paramters one for test method 2: "+testParameters1);
                System.out.println("Paramters two for test method 2: "+testParameters2);
        }
        
        
}

Testng XML:




        
                
                        
                                
                                
                                        
                                        
                                                
                                                
                                        
                                        
                                        
                                                
                                            
                                
                        
                
         
 

Note here, unlike way of passing parameters at suite and test level, we need to pass parameters at method level after include tag.

Output:

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