@Test level parameters in testng.xml

111 views
Skip to first unread message

akshay....@agrostar.in

unread,
Jun 12, 2017, 8:10:23 AM6/12/17
to testng-users
Hello,

I'm wondering if there's a way to provide @Test level parameters through testng.xml file.

e.g. I know we could do something like this, in case of which the supplied parameters are applied to every @Test.

   <test name="Test1">
   
<parameter name="someParam" value="someValue" />
   
<parameter name="someOtherParam" value="someOtherValue" />
   
<parameter name="someParam2" value="someValue2" />

     
<classes>
         
<class name="temp.Temp">
           
<methods>
               
<include name="test1" />
           
</methods>
         
</class>
     
</classes>
   
</test>
   
<test name="Test2">
     
<classes>
         
<class name="temp.Temp">
           
<methods>
               
<include name="test2" />
           
</methods>
         
</class>
     
</classes>
   
</test>

I'm wondering if we could do something like this:

   <test name="Test1">
   
<parameter name="someParam" value="someValue" />
   
<parameter name="someOtherParam" value="someOtherValue" />
     
<classes>
         
<class name="temp.Temp">
           
<methods>
               
<include name="test1" />
           
</methods>
         
</class>
     
</classes>
   
</test>
   
<test name="Test2">
   
<parameter name="someParam2" value="someValue2" />
     
<classes>
         
<class name="temp.Temp">
           
<methods>
               
<include name="test2" />
           
</methods>
         
</class>
     
</classes>
   
</test>

The reason I'm looking for something like this is that each of my @Test tests in my test class need different set of parameters/values.

Thanks,
Akshay

Krishnan Mahadevan

unread,
Jun 12, 2017, 11:01:14 AM6/12/17
to testng...@googlegroups.com

Akshay,

 

It should work. Here’s a sample

 

package com.rationaleemotions.googleforums.params;

 

import org.testng.Assert;

import org.testng.Reporter;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

public class TestClass {

    @Test

    @Parameters({"name", "age"})

    public void testMethod(String name, int age) {

        Reporter.log(name + " is " + age + " years old", true);

        Assert.assertNotNull(name);

        Assert.assertTrue(age > 0);

    }

}

 

Suite file :

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="1265_Suite" parallel="false" verbose="2">

    <parameter name="name" value="Jack"/>

    <test name="partOne" parallel="false" preserve-order="true">

        <parameter name="age" value="25"/>

        <classes>

            <class name="com.rationaleemotions.googleforums.params.TestClass"/>

        </classes>

    </test>

    <test name="partTwo" parallel="false" preserve-order="true">

        <parameter name="age" value="30"/>

        <parameter name="name" value="Jill"/>

        <classes>

            <class name="com.rationaleemotions.googleforums.params.TestClass"/>

        </classes>

    </test>

</suite>

 

 

Output

 

...

... TestNG 6.11 by Cédric Beust (ced...@beust.com)

...

Jack is 25 years old

Jill is 30 years old

 

===============================================

1265_Suite

Total tests run: 2, Failures: 0, Skips: 0

===============================================

 

 

Thanks & Regards

Krishnan Mahadevan

 

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

akshay....@agrostar.in

unread,
Jun 13, 2017, 12:21:43 AM6/13/17
to testng-users
Hi Krishnan,

I think I did not explain the problem correctly. I meant, I'm looking for a way to have <method> level parameters.

e.g. Imagine a Java class having three @Test methods and each of them takes different sets of parameters altogether. I would like to know if there's some way for me to designate respective parameters at <method> level in my testng.xml file.

Krishnan Mahadevan

unread,
Jun 13, 2017, 12:24:23 AM6/13/17
to testng...@googlegroups.com

Akshay,

 

Thanks for clarifying your usecase. No AFAIK, I don’t think you can pass in <parameters> to <method> via a TestNG suite xml file.

 

That said and done, you can very well achieve that using a @DataProvider annotation. Compared to passing parameters via <parameters> tag, it’s a lot more flexible, doesn’t involve an elaborately built suite xml file and gives you the luxury of making your parameters more dynamic. Wouldn’t that work for you ?

akshay....@agrostar.in

unread,
Jun 13, 2017, 12:36:29 AM6/13/17
to testng-users
It seems I can do that. Please check this link (comment by drapostolos), and it very well works for me. Please check the code sample below.

Test class:

package temp;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Temp {

 
@Test()
 
@Parameters ({"param","methodSpecificParam"})
 
public void test(String key1, String key2) {
 
System.out.println(key1+" "+key2);
 
}
}


Suite file:
   <test name="Test1">
   
<parameter name="param" value="globalValue" />

     
<classes>
         
<class name="temp.Temp">
           
<methods>

               
<parameter name="methodSpecificParam"  value="methodSpecificValue"/>
               
<parameter name="param"  value="overridenValue"/>

               
<include name="test1" />
           
</methods>
         
</class>
     
</classes>
   
</test>


Output: 
overridenValue methodSpecificValue

Hope this helps someone in future.

Regarding @DataProvider, incorporating that would need me to make a lot of code changes in order to the test flow and parameters configurable, and I do not really need a complex structure for the same.

akshay....@agrostar.in

unread,
Jun 13, 2017, 12:40:05 AM6/13/17
to testng-users
Fixing the subject line.

    <test <span style="font-size:10.0pt;font-family:&qu

Krishnan Mahadevan

unread,
Jun 13, 2017, 1:38:02 AM6/13/17
to testng...@googlegroups.com

Great. Thanks for sharing that information. To be honest, I wasn’t aware of that till now!

Glad that this resolves your query.

Reply all
Reply to author
Forward
0 new messages