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:
<?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="Test"> <parameter name="confBeforeParameter" value="confBeforeParameter" /> <parameter name="testParameters1" value="testParameters1" /> <parameter name="testParameters2" value="testParameters2" /> <parameter name="confAfterParameter" value="confAfterParameter" /> <parameter name="confBeforeParameter4" value="confBeforeParameter" /> <parameter name="testParameters14" value="testParameters1" /> <parameter name="testParameters24" value="testParameters2" /> <parameter name="confAfterParameter4" value="confAfterParameter" /> <parameter name="confBeforeParameter5" value="confBeforeParameter" /> <parameter name="testParameters15" value="testParameters1" /> <parameter name="testParameters25" value="testParameters2" /> <parameter name="confAfterParameter5" value="confAfterParameter" /> <classes> <class name="Parameters.ParametersExampleAtClonstructor" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
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:
<?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="Test"> <classes> <class name="Parameters.ParametersAtMethodLevel"> <methods> <!-- Including individual method --> <include name="testMethod1"></include> <!-- Passing parameters for first method--> <parameter name="testParameters1" value="testParameters 1" /> <parameter name="testParameters2" value="testParameters 2" /> <!-- Including another method --> <include name="testMethod2"></include> <!-- Passing parameters for second method--> <parameter name="testParameters3" value="testParameters 3" /> <parameter name="testParameters4" value="testParameters 4" /> </methods> </class> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
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
Hello Folks, You might see below dialog box when you download any file in Chrome browser and it finds that…
Hello Folks, Every browse has its default download directory. Whenever you download a file, it gets downloaded in default download…
Hello Folks, In previous post, we have seen that how can we categorised HTTP methods in Safe and Unsafe methods.…
This program was asked in Siemens. Problem Statement: You need to find missing alphabets from a given string. E.g. User…
Hello guys, In previous post, we have seen Introduction of HTTP methods. In this post, we will see what is…
Let's start with a real time example. You must have heard about Facebook. To create an account of Facebook, you need…