How to pass class name dynamically in to class tag in Testng XML

Satish Rongala :

I need to pass the class name dynamically in to <Class> in Testng XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test thread-count="5" name="Test">
      <parameter name="browser" value="chrome"></parameter>
    <classes>
      <class name="testCaseWithKeywordFramework.TestCase1_Login"/>
    </classes>
  </test> <!-- Test -->

</suite> <!-- Suite -->

in the above XML code i want to pass the class name dynamically, can anyone help me in this.

cruisepandey :

You can invoke TestNG from your own programs very easily:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();

This example creates a TestNG object and runs the test class Run2.

For more you can refer here

Similarly, you can invoke TestNG on a testng.xml file or you can create a virtual testng.xml file yourself

For example, suppose you want to create the following virtual file:

<suite name="TmpSuite" >
  <test name="TmpTest" >
    <classes>
      <class name="test.failures.Child"  />
    <classes>
    </test>
</suite>

You would use the following code:

XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;  

And then you can pass this XmlSuite to TestNG:

List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104353&siteId=1