From | raj priya<raj.rp...@gmail.com> |
Date | 09/28/2022 22:57 |
To | testng-users<testng...@googlegroups.com> |
Subject | [testng-users] How to Create testng.xml dynamically? |
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/1664378377439.tfkqpkyt3rglmnzjk2gu0qir%40android.mail.163.com.
package com.rationaleemotions.runtime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class TestRunner {
public static void main(String[] args) {
List<String> classes = getClassesToRun();
if (classes.isEmpty()) {
throw new IllegalStateException("There were no test classes that were found to be executed");
}
XmlSuite xmlSuite = new XmlSuite();
xmlSuite.setVerbose(2);
xmlSuite.setName("My_Test_Suite"); //This is how you set the Test Suite Name.
List<XmlClass> xmlClasses = classes.stream()
.map(XmlClass::new)
.collect(Collectors.toList());
XmlTest xmlTest = new XmlTest(xmlSuite);
xmlTest.setXmlClasses(xmlClasses);
TestNG testng = new TestNG();
testng.setXmlSuites(Collections.singletonList(xmlSuite));
//Since we are using the TestNG APIs we dont need to create a suite file.
//We can directly execute the tests via the TestNG api.
testng.run(); // This executes your tests.
int exitCode = testng.getStatus();
if (exitCode != 0) {
throw new IllegalStateException("Not all tests ran successfully");
}
}
private static List<String> getClassesToRun() {
//Include the following logic here
//1. Read from Excel
//2. Filter out ONLY those rows that have the "execute flag" column set to "TRUE"
//3. Return the list of fully qualified test class names as a list.
return Arrays.asList(
"com.rationaleemotions.runtime.TestClassOne",
"com.rationaleemotions.runtime.TestClassTwo"
);
}
}