How to add tests programmatically to current suite xml

34 views
Skip to first unread message

Velpula Ramu

unread,
Oct 26, 2022, 8:22:41 AM10/26/22
to testng-users
public class Testxmlusingcode {

    public void testNgXmlsuite() {
        List<XmlSuite>suites=new ArrayList<XmlSuite>();//This class describes the tag <suite> in testng.xml.
        List<XmlClass>classes=new ArrayList<XmlClass>();//This class describes the tag <class> in testng.xml.

        List<Class<? extends ITestNGListener>> listenerclasses=new ArrayList<>();

        XmlSuite suite=new XmlSuite();
        suite.setName("Testsuite");
        XmlTest test=new XmlTest(suite);//This class describes the tag <test> in testng.xml
        test.setName("Programingtest");
       
        XmlClass classone=new XmlClass("com.Testng.code.SampleProgramone");
        classes.add(classone);
        XmlClass classtwo=new XmlClass("com.Testng.code.SampleProgramTwo");
        classes.add(classtwo);
        XmlClass classthree=new XmlClass("com.Testng.code.Myprovidencetest");
        classes.add(classthree);
       
        listenerclasses.add(Listenenertest.class);
        test.setXmlClasses(classes);//Sets the XML Classes.
        suites.add(suite);
       
        TestNG tng=new TestNG(); /*This class is the main entry point for running tests in the TestNG framework. Users can createtheir own TestNG object and invoke it in many different ways:
                                   •On an existing testng.xml
                                 •On a synthetic testng.xml, created entirely from Java
                                 •By directly setting the test classes*/

        tng.setXmlSuites(suites);//Specifies the XmlSuite objects to run
        tng.setListenerClasses(listenerclasses);//Define which listeners to user for this run
        tng.run();
    }
    public static void main(String[] args) {
        Testxmlusingcode testmethod=new Testxmlusingcode();
        testmethod.testNgXmlsuite();
    }

⇜Krishnan Mahadevan⇝

unread,
Oct 26, 2022, 10:36:03 AM10/26/22
to testng...@googlegroups.com
It's not clear what you are looking for based on the sample that you shared. Can you please elaborate what you are looking for as information ?

On a side note, you can explore using the TestNG listener IAlterSuiteListener that can help you alter an existing XmlSuite (or) an XmlTest object.

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 Scribblings @ https://rationaleemotions.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 view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/d76f0bea-9511-412d-8222-bb55afef1aacn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages