Can we set dataProvider and dataProviderClass in XML testng or Suite level outside.

95 views
Skip to first unread message

nikhi...@agrostar.in

unread,
Nov 21, 2017, 12:37:53 AM11/21/17
to testng-users
Hello,

In below example, dataProvider and dataProviderClass are same for every @Test method.

I have multiple class were dataProvider and dataProviderClass are same. Can we set dataProvider and dataProviderClass in XML testng or Suite level outside?

@Test(priority = 1, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test1(LinkedHashMap<String, String> data) {

}

@Test(priority = 2, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test2(LinkedHashMap<String, String> data) {

}

@Test(priority =3, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test3(LinkedHashMap<String, String> data) {

}

⇜Krishnan Mahadevan⇝

unread,
Nov 21, 2017, 12:41:55 AM11/21/17
to testng...@googlegroups.com

Why not set them via a listener implementation of org.testng.IAnnotationTransformer2 and then plug in this listener using <listeners> tag or an SPI (Service Loader implementation)


--
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.
--

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/

nikhi...@agrostar.in

unread,
Nov 21, 2017, 4:17:34 AM11/21/17
to testng-users
Can you please give me an example.

Thanks in advance.

Krishnan Mahadevan

unread,
Nov 22, 2017, 11:16:42 PM11/22/17
to testng...@googlegroups.com

public static class DataProviderAdder implements IAnnotationTransformer {

 

    @Override

    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

        if (annotation != null) {

            annotation.setDataProvider("someDataProvider");

            annotation.setDataProviderClass(SampleClass.class);

        }

    }

}

 

This should get you started.

 

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/

 

nikhi...@agrostar.in

unread,
Nov 23, 2017, 3:04:50 AM11/23/17
to testng-users
Thank you Krishnan.

Above code work for me in 6.8 version of testng but fails in 6.11 version.

Below code used for generating XML FIle:

static List<Class> listenerClasses = new ArrayList<Class>();

public static List<XmlSuite> genarateXmlFile() {
XmlSuite suite = new XmlSuite();
suite.setName(Constant.BaseTestHelperConstant.SUITENAME);

XmlTest test = new XmlTest(suite);

test.setName(Constant.BaseTestHelperConstant.TESTCASENAME);

test.setGroupByInstances(true);

List<XmlClass> classes = new ArrayList<XmlClass>();
for (int i = 0; i < Common.BaseTestHelperClass.sheetsName.size(); i++)
classes.add(new XmlClass(Constant.BaseTestHelperConstant.TESTSUITEPATH + Common.BaseTestHelperClass.sheetsName.get(i)));

listenerClasses.add(main.java.com.agrostar.utility.DataProviderAdder.class);

test.setXmlClasses(classes);

List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);

System.out.println("Printing TestNG Suite Xml");
System.out.println(suite.toXml());

return suites;

}

public static void runXmlSuite(List<XmlSuite> suites) {
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.setListenerClasses(listenerClasses);
tng.run();
}

Exception in 6.11 testng:  incompatible types: java.util.List<java.lang.Class> cannot be converted to java.util.List<java.lang.Class<? extends org.testng.ITestNGListener>>

Please let me know if you have any workaround.

Thanks for your help.

⇜Krishnan Mahadevan⇝

unread,
Nov 23, 2017, 3:16:20 AM11/23/17
to testng...@googlegroups.com

Either use tng.addListener() or
Change listenerClasses definition to :

List<Class<? extends ITestNGListener>> = new Array list<>()

nikhi...@agrostar.in

unread,
Nov 27, 2017, 2:04:39 AM11/27/17
to testng-users
Thank you Krishnan.

nikhi...@agrostar.in

unread,
Dec 19, 2017, 1:11:02 AM12/19/17
to testng-users
Solution:

set dataProvider and dataProviderClass in XML testng or Suite level outside.

package main.java.com.agrostar.utility;



import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class DataProviderAdder implements IAnnotationTransformer {


@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

if (annotation != null) {

            annotation.setDataProvider("getData");

annotation.setDataProviderClass(DataUtil.class);

}

}
}




Generate XML using below code:

public static List<XmlSuite> genarateXmlFile() {
    List<XmlTest> tests = new ArrayList<XmlTest>();
    List<XmlSuite> suites = new ArrayList<XmlSuite>();

    XmlSuite suite = new XmlSuite();
suite.setName(Constant.BaseTestHelperConstant.SUITENAME);
    suite.setPreserveOrder(true);

for (int i = 0; i < Common.BaseTestHelperClass.sheetsName.size(); i++) {

XmlTest test = new XmlTest(suite);
        List<XmlClass> classes = new ArrayList<XmlClass>();
        test.setName(Common.BaseTestHelperClass.sheetsName.get(i));
classes.add(new XmlClass(Constant.BaseTestHelperConstant.TESTSUITEPATH + Common.BaseTestHelperClass.sheetsName.get(i)));
test.setXmlClasses(classes);
tests.add(test);
}

listenerClasses.add(main.java.com.agrostar.utility.DataProviderAdder.class);

suites.add(suite);

System.out.println("Printing TestNG Suite Xml");
System.out.println(suite.toXml());
    System.out.println("suites---------" + suites);

return suites;

}

tota...@gmail.com

unread,
Mar 3, 2018, 7:38:10 PM3/3/18
to testng-users
Please refer to the Usage of Dataprovider in the below link with real time examples.

Thanks,
Reply all
Reply to author
Forward
0 new messages