Hello Folks,
Arguments or Parameters help us to achieve reusability of codes and cleanliness of codes. We can do overloading of methods using different number, types and order of parameters to serve differently for different set of values.
TestNG also allows you to parameterized your tests. We will see how can we achieve parameterisation of methods in a TestNG class.
Some real time examples are as below:
You can parameterized @Test annotated methods as well as @BeforeXXXX @AfterXXXX methods. It is not like that you can parameterized on test methods. It is an interview question.
Let’ create some configuration methods and test methods with parameters:
package Parameters; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class ParametersThroughXML { @BeforeMethod public void configurationBeforeMethod(String confBeforeParameter) { System.out.println("Paramters for before configuration method: "+confBeforeParameter); } @Test public void testMethod(String testParameters) { System.out.println("Paramters for test method: "+testParameters); } @AfterMethod public void configurationAfterMethod(String confAfterParameter) { System.out.println("Paramters for after configuration method: "+confAfterParameter); } }
Run above testng class as a testng suite:
Got some error as below??
I see many people get this error but they can not understand why they are getting this. So when you run any test class which has parameterized methods without parameterized testng xml, you will get above error message.
There are two ways of running parameterized methods of a testng class. They are:
Step 1: Update your parameterized methods with @Parameters annotation:
To provide a parameter from testng xml, you need to use Parameters annotation with method. Remember a method can have multiple annotations.
Example:
package Parameters; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class ParametersThroughXML { /* We need to add Parameters annotation to read value from testng xml. Note here that * attribute name passed in Parameters must be same as testng xml. */ @Parameters({"confBeforeParameter"}) @BeforeMethod public void configurationBeforeMethod(String confBeforeParameter) { System.out.println("Paramters for before configuration method: "+confBeforeParameter); } @Parameters({"testParameters1","testParameters2"}) @Test 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); } @Parameters({"confAfterParameter"}) @AfterMethod public void configurationAfterMethod(String confAfterParameter) { System.out.println("Paramters for after configuration method: "+confAfterParameter); } }
Notes:
Step 2: Update your testng xml with parameter tags:
Update your testng xml with parameter tags as below for all parameters of methods in testng class as below:
Understand mapping of parameterized methods of testng call with testng xml below:
Now run testng xml. Please note you must need to run from testng xml otherwise you will get injection exception again.
Output:
Hope it will be clear for you how can we use Parameters annotation in a TestNG class with methods and parameter tag in testng xml to achieve parameterization of methods in testng.
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
"How much Java I need to learn for selenium with Java binding?" is mostly asked question by a Professional who…
Hello Guys, You should not be able to type alphabets or special characters in a field which supposed to accept…
Hello Folks, Recently a guy asked me this question which he was asked in an interview in IBM. What the…
Hello Folks, As part of our API Testing series, we will see “Sending GET request with params in Postman”. In last…
We have learnt in previous posts regarding establishing relationship between test methods. You can go through them below: Dependency in…
In previous post, We have learnt to Establish dependency among test methods. In this post, we will see another concept…