C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
TestNG Annotation AttributesTestNG Parameters are the arguments that we pass to the test methods. There are two ways through which we can pass the parameters to the test methods:
In this topic, we will learn about the TestNG Parameters. We will learn about the parameterization in the xml file. Suppose we want to set the global variables such url settings, username, password or API Keys, there are some values which are constant in all the test cases, in such case we use the TestNG Parameters. TestNG Parameters are present in the xml file. They can be applied either inside the Let's understand through an example. First case: When Parameters are applied below the Step 1: Open the Eclipse. Step 2: We create three class files, i.e., Sum.java, Subtract.java, and Multiply.java. Sum.java package com.TheDeveloperBlog; import org.testng.annotations.Test; import org.testng.annotations.Parameters; public class Sum { @Test @Parameters({"a","b"}) public void add(int c, int d) { int sum=c+d; System.out.println("Sum of two numbers : "+sum); } } Subtract.java package com.TheDeveloperBlog; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class Subtract { @Test @Parameters({"a","b"}) public void add(int c, int d) { int subtract=c-d; System.out.println("Subtraction of two numbers : "+subtract); } } Multiply.java package com.TheDeveloperBlog; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class Multiply { @Test @Parameters({"a","b"}) public void add(int c, int d) { int mul=c*d; System.out.println("Multiplication of two numbers : "+mul); } } Step 3: Now, we create the testng.xml file. testng.xml In the above testng.xml file, we pass the parameters which are valid to all the classes. Step 4: Run the testng.xml file. Output Second case: When parameters are Step 1: Open the Eclipse. Step 2: We create two class files. i.e., Fruits.java and Vegetable.java. Fruits.java package com.TheDeveloperBlog; import org.testng.annotations.Test; import org.testng.annotations.Parameters; public class Fruits { @Test @Parameters("mango") public void mango(String m) { System.out.println("Fruits names are: "); System.out.println(m); } @Test @Parameters("orange") public void orange(String o) { System.out.println(o); } } Vegetable.java package com.TheDeveloperBlog; import org.testng.annotations.Test; import org.testng.annotations.Parameters; public class Vegetable { @Test @Parameters("Cauliflower") public void c(String m) { System.out.println("Vegetable names are :"); System.out.println(m); } @Test @Parameters("Ladyfinger") public void orange(String l) { System.out.println(l); } } Step 3: Now, we create the testng.xml file. In the above testng.xml, we specify the parameters in a particular Step 4: Run the testng.xml file. Output
Next TopicTestNG Listeners
|