How to pass parameters from testng.xml into test case.

by admin on September 3, 2014

TestNG lets you pass parameters directly to your test methods in two different ways:

  • With testng.xml
  • With Data Providers

Passing Parameters with testng.xml

With this technique, you define the simple parameters in the testng.xml file and then reference those parameters in source files. Let us see an example below on how to use this technique to pass parameters.

CREATE TEST CASE CLASS

  • Create a java test class say ParameterizedTest1.java.
  • Add test method parameterTest() to your test class. This method takes a String as input parameter.
  • Add the annotation @Parameters(“myName”) to this method. The parameter would be passed a values from testng.xml which we will see in the next step.

Create the java class file name ParameterizedTest1.java in C:\ > TestNG_WORKSPACE

import org.testng.annotations.Parameters;import org.testng.annotations.Test;publicclassParameterizedTest1{@Test@Parameters("myName")publicvoid parameterTest(String myName){System.out.println("Parameterized value is : "+ myName);}}

CREATE TESTNG.XML

Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suitename="Suite1"><testname="test1"><parametername="myName"value="manisha"/><classes><classname="ParameterizedTest1"/></classes></test></suite>

We can also define the parameters at the <suite> level. Suppose we have defined myName at both <suite> and <test> levels then, in such cases regular scoping rules apply. This means that any class inside <test> tag will see the value of parameter defined in <test>, while the classes in the rest of the testng.xml file will see the value defined in <suite>.

Compile the Test case class using javac.

C:\TestNG_WORKSPACE>javac ParameterizedTest1.java

Now, run the testng.xml, which will run the parameterTest method. TestNG will try to find a parameter named myName first in the <test> tag, and then, if it can’t find it, it searches in the <suit> tag that encloses it.

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

Verify the output.

Parameterized value is : manisha

===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================

TestNG will automatically try to convert the value specified in testng.xml to the type of your parameter. Here are the types supported:

  • String
  • int/Integer
  • boolean/Boolean
  • byte/Byte
  • char/Character
  • double/Double
  • float/Float
  • long/Long
  • short/Short

Passing Parameters with Dataproviders

When you need to pass complex parameters or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc…), in such cases parameters can be passed using Dataproviders. A Data Provider is a method annotated with @DataProvider. This annotation has only one string attribute: its name. If the name is not supplied, the Data Provider’s name automatically defaults to the method’s name. A Data Provider returns an array of objects.

Let us check out examples below of using Dataproviders. The first example is about @DataProvider using Vector, String or Integer as parameter and the second example is about @DataProvider using object as parameter.

EXAMPLE 1

Here, the @DataProvider passes Integer and Boolean as parameter.

CREATE JAVA CLASS

Create a java class PrimeNumberChecker.java. This class checks if the number is prime. Create this class in C:\ > TestNG_WORKSPACE

publicclassPrimeNumberChecker{publicBoolean validate(finalInteger primeNumber){for(int i =2; i <(primeNumber /2); i++){if(primeNumber % i ==0){returnfalse;}}returntrue;}}

CREATE TEST CASE CLASS

  • Create a java test class say ParamTestWithDataProvider1.java.
  • Define the method primeNumbers() which is defined as a Dataprovider using the annotation. This method returns array of object array.
  • Add test method testPrimeNumberChecker() to your test class. This method takes a Integer and Boolean as input parameters. This method validates if the parameter passed is a prime number.
  • Add the annotation @Test(dataProvider = “test1”) to this method. The attribute dataProvider is mapped to “test1”.

Create the java class file name ParamTestWithDataProvider1.java in C:\ > TestNG_WORKSPACE

import org.testng.Assert;import org.testng.annotations.BeforeMethod;import org.testng.annotations.DataProvider;import org.testng.annotations.Test;publicclassParamTestWithDataProvider1{privatePrimeNumberChecker primeNumberChecker;@BeforeMethodpublicvoid initialize(){
        primeNumberChecker =newPrimeNumberChecker();}@DataProvider(name ="test1")publicstaticObject[][] primeNumbers(){returnnewObject[][]{{2,true},{6,false},{19,true},{22,false},{23,true}};}// This test will run 4 times since we have 5 parameters defined@Test(dataProvider ="test1")publicvoid testPrimeNumberChecker(Integer inputNumber,Boolean expectedResult){System.out.println(inputNumber +" "+ expectedResult);Assert.assertEquals(expectedResult,
        	primeNumberChecker.validate(inputNumber));}}

CREATE TESTNG.XML

Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suitename="Suite1"><testname="test1"><classes><classname="ParamTestWithDataProvider1"/></classes></test></suite>

Compile the Test case class using javac.

C:\TestNG_WORKSPACE>.javac ParamTestWithDataProvider1.java PrimeNumberChecker.java

Now, run the testng.xml.

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

Verify the output.

2 true
6 false
19 true
22 false
23 true

===============================================
Suite1
Total tests run: 5, Failures: 0, Skips: 0
===============================================

EXAMPLE 2

Here, the @DataProvider passes Object as parameter.

CREATE JAVA CLASS

Create a java class Bean.java, which is simple object with get/set methods, in C:\ > TestNG_WORKSPACE.

publicclassBean{privateString val;privateint i;publicBean(String val,int i){this.val=val;this.i=i;}publicString getVal(){return val;}publicvoid setVal(String val){this.val = val;}publicint getI(){return i;}publicvoid setI(int i){this.i = i;}}

CREATE TEST CASE CLASS

  • Create a java test class say ParamTestWithDataProvider2.java.
  • Define the method primeNumbers() which is defined as a Dataprovider using the annotation. This method returns array of object array.
  • Add test method testMethod() to your test class. This method takes object bean as parameter.
  • Add the annotation @Test(dataProvider = “test1”) to this method. The attribute dataProvider is mapped to “test1”.

Create the java class file name ParamTestWithDataProvider2.java in C:\ > TestNG_WORKSPACE

import org.testng.annotations.DataProvider;import org.testng.annotations.Test;publicclassParamTestWithDataProvider2{@DataProvider(name ="test1")publicstaticObject[][] primeNumbers(){returnnewObject[][]{{newBean("hi I am the bean",111)}};}@Test(dataProvider ="test1")publicvoid testMethod(Bean myBean){System.out.println(myBean.getVal()+" "+ myBean.getI());}}

CREATE TESTNG.XML

Create a testng.xml C:\ > TestNG_WORKSPACE to execute Test case(s).

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suitename="Suite1"><testname="test1"><classes><classname="ParamTestWithDataProvider2"/></classes></test></suite>

Compile the Test case class using javac.

C:\TestNG_WORKSPACE>javac ParamTestWithDataProvider2.java Bean.java

Now, run the testng.xml.

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

Verify the output.

hi I am the bean 111

===============================================
Suite1
Total tests run: 1, Failures: 0, Skips: 0
===============================================
#SQASolutionShare on FacebookShare on Google+Share on LinkedInTweet about this on TwitterEmail this to someone

Previous post:

Next post: