TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

TestNG Parameters

TestNG Parameters with Testng Tutorial, Introduction, Testng Installation, Features of Testng, Suite Test, Testng Exception Test, Group Test, Plug with Ant, Plug with Eclipse, etc.

<< Back to TESTNG

TestNG Annotation Attributes

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

  • TestNG Parameters
  • TestNG DataProviders

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 tag or tag. If we want to apply the parameters to all the test cases, then the parameters are applied inside the tag. If the parameter is specific to a particular folder, then the parameter is applied within a tag.

Let's understand through an example.

First case: When Parameters are applied below the tag.

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

TestNG Parameters

Second case: When parameters are specific.

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 folder means that the parameters are applied to those classes which are specific to that folder.

Step 4: Run the testng.xml file.

Output

TestNG Parameters
Next TopicTestNG Listeners




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf